Relative paths in mod_python

I

Ivo van der Sangen

I was wondering if I could use relative paths in a mod_python script. At
the moment I am defining a constant string
"/path/to/dir/where/script/resides". The problem with this is that when
I move the script including files I use to get metadata I have to change
this variable. The same problem occurs when I distribute the script; the
person using it will first have to modify it, which is not what I want. Is
it possible that a mod_python script has as current working directory
the directory where the script resides? At the moment os.getcwd()
returns '/'.
 
J

Jochem Berndsen

Ivo said:
I was wondering if I could use relative paths in a mod_python script. At
the moment I am defining a constant string
"/path/to/dir/where/script/resides".
[snip]

You can use Python's built-in __file__ variable. It is set to the absolute
location of your script. You can then use one of the built-in functions in
the `os' module (I forgot which) to extract the directory your script
resides.

Jochem.
 
S

Steve Holden

Ivo said:
I was wondering if I could use relative paths in a mod_python script. At
the moment I am defining a constant string
"/path/to/dir/where/script/resides". The problem with this is that when
I move the script including files I use to get metadata I have to change
this variable. The same problem occurs when I distribute the script; the
person using it will first have to modify it, which is not what I want. Is
it possible that a mod_python script has as current working directory
the directory where the script resides? At the moment os.getcwd()
returns '/'.

import os
req.write(os.path.abspath(__file__))

seems to give you the absolute path to the module you are executing. You
can use the other os.path primitives to extract the directory from that
and use it to locate the associated data files.

regards
Steve
 
B

Bruno Desthuilliers

Ivo van der Sangen a écrit :
I was wondering if I could use relative paths in a mod_python script. At
the moment I am defining a constant string
"/path/to/dir/where/script/resides". The problem with this is that when
I move the script including files I use to get metadata I have to change
this variable. The same problem occurs when I distribute the script; the
person using it will first have to modify it, which is not what I want. Is
it possible that a mod_python script has as current working directory
the directory where the script resides? At the moment os.getcwd()
returns '/'.

You could set this constant in the apache conf as a PythonOption. And/Or
you could post this question to mod_python's mailing list !-)
 
I

Ivo van der Sangen

Ivo van der Sangen a écrit :

You could set this constant in the apache conf as a PythonOption. And/Or
you could post this question to mod_python's mailing list !-)

I wasn't aware of the mod_python list. The next time I will post my
question there, but I don't think I will get a better answer there; the
solutions given here so far are fine.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top