__file__ vs __FILE__

K

klenwell

I apologize in advance for coming at this from this angle but...

In PHP you have the __FILE__ constant which gives you the value of the
absolute path of the file you're in (as opposed to the main script
file.) With the function dirname, this makes it easy to get the
parent dir of a particular file from within that file:

$parent_dir = dirname(__FILE__);

I'm looking for the best way to accomplish this in Python. This seems
to work:

parent_dir = os.path.normpath(os.path.join(os.path.abspath(__file__),
'..'))

Can anyone confirm the reliability of this method or suggest a better
(one-line) method for accomplishing this?

Thanks,
Tom
 
J

Jeff McNeil

The __file__ attribute is present when you run a script from a file.
If you run from the interactive interpreter, it will raise a
NameError. Likewise, I believe that in earlier versions of Python
(2.1? Pre 2.2?) it was only set within imported modules. I've used the
'os.path.realpath(os.path.pardir)' construct in a couple of scripts
myself. That ought to work within the interactive interpreter.

Jeff
 
B

Bjoern Schliessmann

Jeff said:
I've used the 'os.path.realpath(os.path.pardir)' construct in a
couple of scripts myself.

In Windows? Using Linux, this gives me "..".

I use os.path.dirname(os.path.abspath(__file__))
That ought to work within the interactive interpreter.

Why do you also enter that code in the interpreter? If it is in a
module, you should always be able to use __file__.

Regards,


Björn
 
G

Giampaolo Rodola'

I apologize in advance for coming at this from this angle but...

In PHP you have the __FILE__ constant which gives you the value of the
absolute path of the file you're in (as opposed to the main script
file.) With the function dirname, this makes it easy to get the
parent dir of a particular file from within that file:

$parent_dir = dirname(__FILE__);

I'm looking for the best way to accomplish this in Python. This seems
to work:

parent_dir = os.path.normpath(os.path.join(os.path.abspath(__file__),
'..'))

Can anyone confirm the reliability of this method or suggest a better
(one-line) method for accomplishing this?

Thanks,
Tom

This is not really 'one-line' since you have to import two modules
first, but it looks nicer...:

import sys, os
print sys.argv[0] # absolute file name
print os.path.dirname(sys.argv[0]) # absolute dir name
 
G

Gabriel Genellina

This is not really 'one-line' since you have to import two modules
first, but it looks nicer...:

import sys, os
print sys.argv[0] # absolute file name
print os.path.dirname(sys.argv[0]) # absolute dir name

Note that this returns the location of the *main* script, not the current
module, as the OP explicitely asked for.
 
J

Jeff McNeil

I'm using Mac OS X, and it get:

Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
The __file__ construct is fine from within a module and is probably
more inline with what the OP was looking for anyways.
 
K

klenwell

In Windows? Using Linux, this gives me "..".

I use os.path.dirname(os.path.abspath(__file__))


Why do you also enter that code in the interpreter? If it is in a
module, you should always be able to use __file__.

Regards,

Björn

--
BOFH excuse #238:

You did wha... oh _dear_....

I use os.path.dirname(os.path.abspath(__file__))

That makes sense, as it is almost a literal translation of what I'm
used to using in PHP. Thanks for pointing this out.
 
B

Bjoern Schliessmann

klenwell said:
Bjoern Schliessmann wrote:

That makes sense, as it is almost a literal translation of what
I'm used to using in PHP. Thanks for pointing this out.

You're welcome, happy coding :)

Regards,


Björn
 
G

Giampaolo Rodola'

This is not really 'one-line' since you have to import two modules
first, but it looks nicer...:
import sys, os
print sys.argv[0] # absolute file name
print os.path.dirname(sys.argv[0]) # absolute dir name

Note that this returns the location of the *main* script, not the current
module, as the OP explicitely asked for.

Whoops! You're right.
 
S

sandipm

interestingly...
I wanted to reuse this code so i wrote function in a file

def getParentDir():
import os
return os.path.dirname(os.path.abspath(__file__))


and called this function, in another file, its giving me parent
directory of file where this function is defined.?
how to reuse this piece of code then? or am i doing something wrong?

btw using __path__[0], I can get the parent directory of file too...


sandip




En Sat, 03 Nov 2007 10:07:10 -0300, Giampaolo Rodola' <[email protected]>
escribió:
In PHP you have the __FILE__ constant which gives you the value of the
absolute path of the file you're in (as opposed to the main script
file.)
This is not really 'one-line' since you have to import two modules
first, but it looks nicer...:
import sys, os
print sys.argv[0] # absolute file name
print os.path.dirname(sys.argv[0]) # absolute dir name
Note that this returns the location of the *main* script, not the current
module, as the OP explicitely asked for.

Whoops! You're right.
 
D

Dennis Lee Bieber

interestingly...
I wanted to reuse this code so i wrote function in a file

def getParentDir():
import os
return os.path.dirname(os.path.abspath(__file__))


and called this function, in another file, its giving me parent
directory of file where this function is defined.?
how to reuse this piece of code then? or am i doing something wrong?

I'd suggest you should be passing in an argument...

---- whatever.py
import os.path

def getParentDir(f):
return os.path.dirname(os.path.abspath(f))

---- user.py
import whatever

pd = whatever.getParentDir(__file__)

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
M

Matimus

interestingly...
I wanted to reuse this code so i wrote function in a file

def getParentDir():
import os
return os.path.dirname(os.path.abspath(__file__))

and called this function, in another file, its giving me parent
directory of file where this function is defined.?

This is true. __file__ is defined at the module level where the
function is defined.
how to reuse this piece of code then? or am i doing something wrong?

You have a few choices. You could implement it wherever you need it
which might actually be nice from a OO point of view. You could modify
the function above to accept a parameter and operate on the __file__
attribute of that parameter. Or, you could use the inspect module to
look at the stack and operate on the __file__ attribute of the caller.

The first one is obvious to implement, although it will only apply to
modules you have implemented. The second one I think someone has
already posted a solution to. Here is how to implement the third one
(this is also usable with a parameter).

Code:
import inspect
import os

def getpardir(obj=None):
    if obj is None:
        obj = inspect.stack()[1][0]
    return os.path.dirname(inspect.getfile(obj))

Some may choose to stay away from this sort of thing though, since
inspecting the stack can tend to feel a bit like voo-doo. Passing a
parameter is probably your best bet IMHO.

Matt
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top