equivalent of Ruby's Pathname?

Steve Holden steve at holdenweb.com
Tue Mar 16 18:00:36 CET 2010


Phlip wrote:
> Chris Rebert wrote:
> 
>> The next closest thing would probably be the Python Cookbook:http://code.activestate.com/recipes/langs/python/
> 
> One thing I really like about ... my hacked version of path.py ... is
> path.cd( lambda: ... ). It works great inside fabfile.py to
> temporarily switch to a different folder:
> 
> sample_project = path('sample_project').abspath()
> 
> def run():
>     sample_project.cd( lambda:
>         _sh('python manage.py runserver 0.0.0.0:8000 --
> settings=test_settings') )
> 
> After the lambda runs, we exception-safely return to the home folder.
> 
> (BTW I'm aware that a fabfile.py command with only one statement will
> return to its shell and remain in the correct folder. It's just ...
> the thought!)
> 
> This be .cd():
> 
> class path:
> 
>     def cd(self, block=None):
>         previous = path(os.path.curdir).abspath()
>         self.chdir()
> 
>         if block:
>             try:      block()
>             finally:  previous.chdir()
> 
> That's based on Jason Orendoff's work at http://www.jorendorff.com/articles/python/path
> 
Wouldn't this be better written as a context manager?

Be aware also that this won't work well in a multi-threaded environment
(assuming os.path.chdir is ultimately used to change directories)
because it effects the process's (globaL current directory.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list