468,110 Members | 1,949 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

How to read the directory which the actively running python file islocated in?

Is there a way to read the directory that the currently running python file
is located in?
Cheers
Mike.

Dec 1 '06 #1
4 1503
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.
Dec 1 '06 #2
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.
Dec 1 '06 #3
Michael Malinowski schrieb:
Is there a way to read the directory that the currently running python file
is located in?
Hi Mike!

To get the started program:

sys.argv[0]

Donīt use ``os.curdir``.
To get the filename, of the current module:

__file__
To get the directory:

os.path.split(sys.argv[0])[0]
os.path.split(__file__)[0]
Regards,
Gerold
:-)

--
__________________________________________________ ______________________
Gerold Penz - bcom - Programmierung
ge*********@tirol.utanet.at | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
wirksamsten Erfolgsfaktoren. Dale Carnegie
Dec 1 '06 #4

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

Dec 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chris | last post: by
3 posts views Thread by Larry Bates | last post: by
18 posts views Thread by jas | last post: by
2 posts views Thread by Samuel Yin | last post: by
1 post views Thread by Steven D'Aprano | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.