__file__ vs __FILE__

sandipm sandip.more at gmail.com
Mon Nov 5 04:07:17 EST 2007


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




On Nov 5, 5:09 am, Giampaolo Rodola' <gne... at gmail.com> wrote:
> On 3 Nov, 15:46, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
>
>
>
> > En Sat, 03 Nov 2007 10:07:10 -0300, Giampaolo Rodola' <gne... at gmail.com>  
> > escribió:
>
> > > On 3 Nov, 04:21, klenwell <klenw... at gmail.com> wrote:
> > >> 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.
>
> > --
> > Gabriel Genellina
>
> Whoops! You're right.





More information about the Python-list mailing list