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

T

tekion

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.
 
T

TechieInsite

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

I hope this helps.

Greg
 
T

TechieInsights

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

TechieInsights

Note:
The os.path.relpath is new in 2.6. If you are using an older version
you will have to write your own algorithm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top