[Grok-dev] Re: Relative filesystem path?

Jim Washington jwashin at vt.edu
Sun Sep 16 15:09:27 EDT 2007

Philipp von Weitershausen wrote:
> By the way, I'd spell that
>   loc = os.getcwd()
Thanks!  It's always good to catch a new trick.
> No. First of call, you can't rely on whatever the current working
> directory (CWD) is. Second, you can't rely on where the package you're
> dealing with is installed. grokproject suggests a pretty common
> directory structure, but in the end who knows where people are
> installing eggs...
I was worried about not being able to rely on cwd, and also about the
common directory structure, which is why I asked.
>> Is there a good alternative?  Does grok have a hook I can use for this
>> that would not be so susceptible to breakage?  I've looked, but it seems
>> that the path I need is consumed and made unimportant by martian.
> I hope I'm understanding you right. I assume you want to be able to
> refer to files in a package. So your relative filenames are relative
> to package (much like app_templates, for example, is relative to the
> package that app.py is in).
> The canonical way to do that is to use __file__. For example, let's
> say I'm in foo.py and wanted to read xyz.txt which is next to foo.py:
>   foo_directory = os.path.dirname(__file__)
>   xyz_txt = os.path.join(foo_directory, 'xyz.txt')
Actually, I want the place where the application is, not the library
location, which is why I was using the cwd stuff.  I'm thinking that an
application should be able to do

class Index(mylibrary.LXMLEnabledDescendentOfGrok.View):
    def render(self):
        nav = self.body.get_element_by_id('navigation')
        for k in navItems:
            li = SubElement(nav,'li',{'class':'navitem'})
            a = SubElement(li,'a',{'href':k.href})
            a.text = k.title

where base.html has a div element with an id of 'navigation' and resides
in the 'html' directory which is in the same folder as the app_templates

__file__ would just get me to the files in my library package.  I want
to put the html templates out where the web application is, not inside
the library egg.

More information about the Grok-dev mailing list