[Zope] Can I do this in a python products ??
Thomas B. Passin
tpassin@mitretek.org
Fri, 3 May 2002 10:59:34 -0400
[Nicolas Évrard]
Now, my question :
I do this in the initialization of my product :
fd = open('the_file')
line = fd.readline()
while line :
process_line(line)
line = fd.readline()
The file 'the_file' is in the base directory of the product but I keep
getting 'No such file'. I then decided to put this file on the Zope
Directory where my product will be localized but the same problem arose.
Is it possible to have some kind of a rc-file for a product the need to
be parsed (this file residing either in the product directory or in the
zope directory).
[Tom P]
You should always make sure a module can find out the right location for a
file rather than relying on a guess or default location. When I use
external scripts, I generally use a path relative to the module or package
that contains the program. That way I can move the code to another
installation on another directory tree or drive and still have things work.
There are several possibilities. Here are some:
1) Use an environmental variable. This means you have to remember to set
the variable before running Zope.
2) Put your code into a package (put it into a directory on the python path
and put an __init__.py file into that directory). When you import the
package, you can get the complete path from the package's __file__attribute.
For example, if you have a package called "altlas":
import atlas,os.path
PATH=os.path.dirname(atlas.__file__)
3) A variation on 2) is to create a variable in the package's __init__.py
file:
import os.path
BASEPATH=os.path.dirname(__file__)
Now when you import the package, the BASEPATH variable is available. To do
this, you may need to put your code into subdirectories or subpackages under
the package.
4) You could also try making the path be relative to that of Zope by using
os.cwd() or sys.argv[0], but that might be less robust.
Cheers,
Tom P