Find directory name of file?

V

veracon

I'm pretty new at Python, so I have no idea how to do this: How do I
find the name of the directory that contains the application currently
being executed (e.g. if the file is /home/user/file.py, I want to get
the /home/user part)?

Thanks!
 
J

jmdeschamps

veracon said:
I'm pretty new at Python, so I have no idea how to do this: How do I
find the name of the directory that contains the application currently
being executed (e.g. if the file is /home/user/file.py, I want to get
the /home/user part)?

Thanks!

look into os.path module for help
 
P

Peter Hansen

also the function os.cwd()
;-)

I'm not sure how os.cwd() helps, but generally the solution to this
starts with pointing out that sys.argv[0] usually contains the path to
the main script, which is often the answer needed.

Recipes have been posted in the past which cover a wider range of
situations, including when running as a "py2exe'd" program (i.e.
"frozen"), as the simple solution doesn't cover all cases.

Checking the archives would probably help.

-Peter
 
G

Grant Edwards

also the function os.cwd()

Neither os.path nor os.cwd are going to tell you the location
of the python program being executed.

Looking at the value of sys.argv[0] is probably the best bet.
If it's a relative path, then you can use os.path to convert it
to an absolute path if you like:

Try something like this at the beginning of your program and
see if it does what you want:

print os.path.abspath(sys.argv[0])
 
N

ngw

-veracon said:
I'm pretty new at Python, so I have no idea how to do this: How do I
find the name of the directory that contains the application currently
being executed (e.g. if the file is /home/user/file.py, I want to get
the /home/user part)?

Try os.path.dirname (__file__)

HTH,
ngw
 
F

Fuzzyman

Peter said:
also the function os.cwd()
;-)

I'm not sure how os.cwd() helps, but generally the solution to this
starts with pointing out that sys.argv[0] usually contains the path to
the main script, which is often the answer needed.

Recipes have been posted in the past which cover a wider range of
situations, including when running as a "py2exe'd" program (i.e.
"frozen"), as the simple solution doesn't cover all cases.

Checking the archives would probably help.

There are a couple of functions from `pathutils
<http://www.voidspace.org.uk/python/pathutils.html>`_ that do this
(originally supplied by Thomas Heller I believe) :

def main_is_frozen():
"""Return ``True`` if we're running from a frozen program."""
import imp
return (
# new py2exe
hasattr(sys, "frozen") or
# tools/freeze
imp.is_frozen("__main__"))

def get_main_dir():
"""Return the script directory - whether we're frozen or not."""
if main_is_frozen():
return os.path.abspath(os.path.dirname(sys.executable))
return os.path.abspath(os.path.dirname(sys.argv[0]))

just call ``get_main_dir()`` and it works consistently with py2exe'd
scripts and normal scripts.

All the best,


Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
G

gene tani

ngw said:
Try os.path.dirname (__file__)

what O/S and python release are you using? I get NameError: '__file__'
not defined in win32 and OS X (both are 2.4.x, installed from
ActiveState installers)
 
B

Blair P. Houghton

Grant said:
Try something like this at the beginning of your program and
see if it does what you want:

print os.path.abspath(sys.argv[0])

Wanna see something freaky?

In IDLE, I type the following:
>>> import sys
>>> import os.path
>>> os.path.abspath(sys.argv[0]) 'C:\\Program Files\\Python\\2.4'
>>>

Pretty normal, right? Then I type this:

Yup. No output. Even if I wrap the sys.argv[0] in something visible,
there's nothing there:
>>> print "x"+str(sys.argv[0])+"y" xy
>>>

How does it know what the path of the argument is if it doesn't know
what the argument is? Seems a bit presumptive to assume that CWD is
the path to a blank string...unless abspath presumes that anything you
pass it is an executable, even if it's got no bytes...

--Blair
 
P

Peter Hansen

Blair said:
Wanna see something freaky?

In IDLE, I type the following:
import sys
import os.path
os.path.abspath(sys.argv[0])
'C:\\Program Files\\Python\\2.4'

Pretty normal, right?

Depends on your definition of normal. That doesn't look to me like it's
the path to IDLE. Anyway, sys.argv[0] is probably documented as being
undefined when you run an interactive session...
> Then I type this:

Yup. No output. Even if I wrap the sys.argv[0] in something visible,

The correct way to see what's really in a string is to call repr() on
it, as in "print repr(sys.argv[0])". That would show you that it's
merely the empty string ''.
How does it know what the path of the argument is if it doesn't know
what the argument is? Seems a bit presumptive to assume that CWD is
the path to a blank string...unless abspath presumes that anything you
pass it is an executable, even if it's got no bytes...

os.path.abspath('') is merely returning the current directory. It does
seem sort of implicit, but it's doubtless behaviour that is counted on
by many applications at this point, so I'm afraid it's unlikely to
change, even if it wasn't such a good idea. (And it might well be an
excellent idea...)

-Peter
 

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

Staff online

Members online

Forum statistics

Threads
474,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top