is there a way to determine a relative path to the script?

TechieInsite GDoermann at
Fri Jan 2 21:46:00 CET 2009

import os
base = __file__.split(os.sep)
os.path.relpath('path/to/your/file/, base)

I hope this helps.


tekion wrote:
> Hello,
> I have a script in /usr/local/app/ and a configuration file
> relative to /usr/local/app/conf.  When I call the script with an
> absolute path of /usr/local/app/  I recieved an error
> similar to the below error:
> Traceback (most recent call last):
>   File "script/art/", line 28, in ?
>     database =  Config.get("DB", "user")
>   File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
> python2.3/", line 505, in get
>     raise NoSectionError(section)
> ConfigParser.NoSectionError: No section: 'DB'
> I know why, the configuration which I reference in the script is
> relative to "/usr/local/app", when I call the script via an absolute
> path, then the relative the configuration file is base on where ever I
> call the script from. One way to fix this is to add a path manually
> into the variable. But I would like to avoid this hard-coding
> parameter into my script. Is there a way to determined the relative
> location of the script programatically? FYI, in the end this scrip
> would run from CRON.

More information about the Python-list mailing list