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.
In Python, it __file__identifies the current Python file. Thus:
__file__
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.
How about using sys.path [0]
- 'print os.path.join(sys.path [0], sys.argv [0])'
https://docs.python.org/library/sys.html