newbie question: how to get directory of script?

animeshk animeshk at yahoo.com
Tue Nov 12 12:57:43 EST 2002


I'm sure this has been asked and answered a few zillion times: sorry!

I want to be able to get the path of the script I am running (the idea
is that I might have a data file or an ini file sitting in the same
directory as the py file).  Okay . . . so how do I get it?  BTW, I am
running Python 2.2.2 under Windows XP, with the wincom extensions.

The docs said that sys.argv[0] should have the name of the script and,
depending on the platform, the full path.  Nope: sys.argv[0] has the
full path of python.exe (this is occurring within the Wing IDE
environment, within PythonWin, and even from the command line).

The docs also said that sys.path[0] should have the directory of the
script.  Again, nope: it has the directory of python.exe.  However, I
saw the directory of the script in sys.path[1].  Problem: this works
fine from the command line and from within the Wing IDE, but not in
PythonWin (PythonWin sets sys.path[1] to '').

So . . . is there some "official"/"safe"/"standard" way of getting
this information?  Thanks!



More information about the Python-list mailing list