Possible Duplicate:
os.path.dirname(__file__) returns empty
A user of a Python script of mine said they got the following error:
Traceback (most recent call last):
File "MCManager.py", line 7, in <module>
os.chdir(os.path.dirname(__file__))
OSError: [Errno 2] No such file or directory: ''
How could the directory the script is in not exist? Is it a compatibility issue? I use the same OS and version as the one with the error, and haven’t been able to replicate this.
It happens in Python 2 when executing a script from the same directory, e.g.
One solution is to use
os.path.abspath(__file__)in the code, so that you can always resolve the script directory in all three usage cases below: