howto obtain directory where current (running) py-file is placed?

Gerard Flanagan grflanagan at yahoo.co.uk
Fri Jun 8 03:01:59 EDT 2007


On Jun 8, 1:01 am, Stef Mientki <S.Mientki-nos... at mailbox.kun.nl>
wrote:
> Gerard Flanagan wrote:
> > On Jun 7, 8:39 am, dmitrey <open... at ukr.net> wrote:
> >> Hi all,
> >> I guess this question was asked many times before, but I don't know
> >> keywords for web search.
>
> >> Thank you in advance, D.
>
> > import os
>
> > d1 = os.path.dirname(__file__)
>
> here I get an error "__file__" is not defined ??

The code must be run from a script or module.
If you run it from an interactive prompt, then you will get a
NameError.

> d2 = os.path.dirname(os.__file__)
>
> here I get a completely different path ??
>

Is it the path of the os module? You can expect pure Python modules to
have a __file__ attribute, but not every module has one - see the
docs.

>
> > print d1
> > print d2
>
> This seems to work (but I doubt it's always working !! )
> print os.getcwd()
>
> so what's the real answer ?
>
> thanks,
> Stef Mientki





More information about the Python-list mailing list