anders wrote:
in os module there is many funktion/methods to extract this information
to ask the path to the current running pythonprogram you can do likes
this
----- CUT-----------
import os
print os.getcwd()
----- CUT ------
// Anders
Michael Malinowski skrev:
Is there a way to read the directory that the currently running python file
is located in?
Cheers
Mike.
os.getcwd() provides the "current working directory". This is *not*
necessarily the directory that the "currently running python file"
[whatever that means] is located in.
Try picking what you really want/need out of this:
C:\junk\foo>type ..\where.py
import sys, os
if __name__ == "__main__":
print "running as script"
else:
print "imported module named", __name__
print "code loaded from file", __file__
print "sys.argv[0] is", sys.argv[0]
print "cwd is", os.getcwd()
C:\junk\foo>..\where.py
running as script
code loaded from file C:\junk\where.py
sys.argv[0] is C:\junk\where.py
cwd is C:\junk\foo
C:\junk\foo>type runwhere.py
import sys
sys.path[0:0] = ['c:\\junk']
import where
C:\junk\foo>runwhere.py
imported module named where
code loaded from file c:\junk\where.py
sys.argv[0] is C:\junk\foo\runwhere.py
cwd is C:\junk\foo
C:\junk\foo>cd ..
C:\junk>md bar
C:\junk>cd bar
C:\junk\bar>..\foo\runwhere.py
imported module named where
code loaded from file c:\junk\where.pyc
sys.argv[0] is C:\junk\foo\runwhere.py
cwd is C:\junk\bar
HTH,
John