[Zope-dev] Product directory?

Fred Drake fred at zope.com
Tue Mar 9 14:16:22 EST 2004


On Tuesday 09 March 2004 01:58 pm, Ian Beatty wrote:
 > This has to be an easy one.

Good, I'll take it.  ;-)

 > From within my Python-based product's code, how do I get access to the
 > product's directory on the filesystem? os.getcwd() seems to provide the
 > working directory of the shell used to launch Zope, at least when running
 > in debug mode.

This isn't actually Zope specific; you can easily get the directory a module 
lives in using this:

    import os
    here = os.path.dirname(os.path.abspath(__file__))

The variable "here" will hold a string with an absolute path to the directory 
containing the source file for your module (named by __file__).  The call to 
os.path.abspath() isn't strictly necessary, but protects against future calls 
to os.chdir() (though not past calls!) in case the module was loaded from 
directory named by a relative directory on sys.path.

You can do this just once at module scope; there's no need to recompute this 
each time you need it.


  -Fred

-- 
Fred L. Drake, Jr.  <fred at zope.com>
PythonLabs at Zope Corporation




More information about the Zope-Dev mailing list