os.chdir() considered harmful

Rob Hooft rob at hooft.net
Mon Sep 25 13:48:03 CEST 2000


 DEW> When I have to use os.chdir() I like to make sure that the old path is
 DEW> restored when I am done.  So I use a class like this:

 DEW> import os

 DEW> class ChangePath:
 DEW>   def __init__(self, newPath):
 DEW>     self.savedPath = os.getcwd()
 DEW>     os.chdir(newPath)

 DEW>   def __del__(self):
 DEW>     os.chdir(self.savedPath)

You better make sure that only one of these is active at any time, or
that they get destroyed in reverse order of creation!

Rob

-- 
=====   rob at hooft.net          http://www.hooft.net/people/rob/  =====
=====   R&D, Nonius BV, Delft  http://www.nonius.nl/             =====
===== PGPid 0xFA19277D ========================== Use Linux! =========



More information about the Python-list mailing list