By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,749 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,749 IT Pros & Developers. It's quick & easy.

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

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

Dec 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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.