How to find the path to the currently executing script?

Duplicate: In Python, how do I get the path and name of the file that is currently executing?

I would like to know the path to the currently executing script. I tried os.getcwd (), but only returns the directory in which I ran the script from a nonexistent directory stored in the script.

+3
source share
2 answers

In Python, it __file__identifies the current Python file. Thus:

print "I'm inside Python file %s" % __file__

prints the current python file. Note that this works in imported Python modules as well as in scripts.

+7
source

How about using sys.path [0]

- 'print os.path.join(sys.path [0], sys.argv [0])'

https://docs.python.org/library/sys.html

+1

All Articles