0

I have the following working directory from which I am running ankiAdd.py - in this script at one point i'm accessing the 'internal API' of another python program elsewhere on my hard drive in order to use some of those functions. I would like to get the working directory where 'ankiAdd.py' is stored however I kept retrieving another directory.

Working directory

The following snippet is a function within my ankiAdd.py script. Collection is imported from another directory on my hard drive in which another program Anki is installed. I'd like the file_pattern variable to give the directory of this script and not the directory of cpath, which is currently happening.

sys.path.append("C:/Users/Shaun/Documents/Personal_Projects/ankiExperiment/anki-scripting/anki")
from anki.storage import Collection
PROFILE_HOME = os.path.expanduser("C:/Users/Shaun/Desktop/User 1")
cpath = os.path.join(PROFILE_HOME, "collection.anki2")

@eel.expose
def callLoad():
    col = Collection(cpath, log=True)
    json_pattern = "word_data.json"
    file_pattern = os.path.join(os.getcwd(),json_pattern)
    load(col,file_pattern)

I've tried numerous things like os.getcwd() and os.path.dirname(os.path.realpath(file)) however I kept ending up retrieving C:\Users\User\Desktop\User 1\collection.media which is the location of cpath. How can I get the directory of ankiAdd.py which is the script which was originally run?

edit:

def callLoad():
    col = Collection(cpath, log=True)
    json_pattern = "word_data.json"
    print(os.path.dirname(os.path.realpath(__file__)))
    file_pattern = os.path.join(os.getcwd(),json_pattern)
    #load(col,file_pattern)

if __name__ == '__main__':
callLoad()

if I run this code, the directory returned is C:\Users\Shaun\Desktop\User 1\collection.media which is the directory set by this at the top of my file:

PROFILE_HOME = os.path.expanduser("C:/Users/Shaun/Desktop/User 1")

I am running the script from

workspace

2 Answers 2

1

If I understand your question correctly, you're trying to get the location of a file or module you're importing, and keep getting the location of the file or module that's doing the import. This can be resolved!

Anything you import will have a __file__ attribute. If you had, for instance,

import anki

print(anki.__file__)

you would get the location of the anki module's __init__.py file (or if anki is just a Python file, you'd get the location of the file itself).

Sign up to request clarification or add additional context in comments.

1 Comment

I'm actually trying to get the location of the .py script in my working directory and using __ file __ I'm getting back this path which I set as a variable PROFILE_HOME = os.path.expanduser("C:/Users/Shaun/Desktop/User 1"). I'd like to get the location of the file from which I call import anki
0

file returns the location of the script in execution. You can try this.

print(__file__)

3 Comments

If I call a function callLoad() from if name == 'main' where I have print(os.path.dirname(os.path.realpath(file))) inside of that function the directory that is returned is not the directory of the .py script that I am running. (Please see my edit)
Can you try this in place of file and share me the output path ? " print(sys.argv[0]) " Note: Please do import "sys" and "os" packages.
Thank you. sys and os are both imported. print(sys.argv[0]) in that function gives back "ankAdd.py" - is there a way to display the full path?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.