[mod_python] Database error and files problem

Graham Dumpleton grahamd at dscpl.com.au
Tue Nov 22 16:54:48 EST 2005

El TuZa wrote ..
> Hi, I'm having 2 different problems. First the most important. I am
> using publisher (modpython 2 and apache 1.3.33) and when i want to
> open a file it jumps with  "IOError: [Errno 2] No such file or
> directory:" no matter the path (i've tried  ./template, ../template,
> ../../template, with template located in the same folder as the .py).

The current working directory could be anything when using mod_python.
Therefore you cannot use relative pathnames. Instead use something like:

  import os

  __here__ = os.path.dirname(__file__)
  filename = os.path.join(__here__,"template")

This finds the directory the .py file is contained in and then appends your
relative path to it to yield an absolute path.

In other contexts, you may want to use req.filename or req.hlist.directory
instead of __file__. It depends on what you want to use as the anchor point
for your relative paths.


More information about the Mod_python mailing list