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

  • Thread starter Michael Malinowski
  • Start date
M

Michael Malinowski

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

anders

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:
 
A

anders

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:
 
G

Gerold Penz

Michael said:
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
(e-mail address removed) | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
wirksamsten Erfolgsfaktoren. Dale Carnegie
 
J

John Machin

anders said:
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:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top