Using os.popen and os.chdir together

Looney, James B james.b.looney at lmco.com
Tue May 8 18:17:28 EDT 2007


Within a script on a *nix machine, I use os.chdir then os.popen, and it
appears to me as though the os.chdir had no effect so far as the
os.popen is concerned.  Why's that?  Here's what I'm doing:

>>> import os
>>> os.path.realpath( os.curdir )
'/home/jlooney'
>>> print os.popen( "echo $PWD" ).readlines()
['/home/jlooney\n']
>>> 
>>> os.chdir( "/tmp" )
>>> os.path.realpath( os.curdir )
'/tmp'
>>> print os.popen( "echo $PWD" ).readlines()
['/home/jlooney\n']
>>> 

You'll notice that initially, the current paths are the same, and
correct.  After I call os.chdir, and try os.popen, it's not in the new
directory.  When I do other things like creating new files, the chdir
did exactly what I expected.  What I don't understand is why os.popen
appears to be special?  How do I change directories within a script and
have popen see that change?

-JB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070508/9d7b29a9/attachment.html>


More information about the Python-list mailing list