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

TechieInsights GDoermann at gmail.com
Fri Jan 2 22:07:56 CET 2009


import os
os.path.relpath('/path/to/your/file', os.path.dirname(__file__))

tekion wrote:
> Hello,
> I have a script in /usr/local/app/mypython.py and a configuration file
> relative to /usr/local/app/conf.  When I call the script with an
> absolute path of /usr/local/app/mypthon.py  I recieved an error
> similar to the below error:
>
> Traceback (most recent call last):
>   File "script/art/auditlog.py", line 28, in ?
>     database =  Config.get("DB", "user")
>   File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
> python2.3/ConfigParser.py", 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