Easier way to get the "here" path?

Andrew alif016 at gmail.com
Sat Jul 26 12:02:14 EDT 2008


bukzor wrote:
> I have to go into these convulsions to get the directory that the
> script is in whenever I need to use relative paths. I was wondering if
> you guys have a better way:
> ...
>   
If you just need the current path (where it is executed) why not use 
os.getcwd()
which returns a string of the absolute path to the module being executed.


$ echo "print __file__" > path.py
$ ipython
In [1]: import path
path.pyc

In [2]: import os

In [3]: os.path.join(os.getcwd(), path.__file__.rstrip("c"))
Out[3]: '/home/andrew/path.py'
--
Andrew



More information about the Python-list mailing list