popen function of os and subprocess modules

Albert Hopkins marduk at letterboxes.org
Thu Oct 29 00:11:11 CET 2009


On Wed, 2009-10-28 at 07:15 -0700, banu wrote:
> 
> Thanks for the reply Jon
> Basically I need to move into a folder and then need to execute some
> shell commands(make etc.) in that folder. I just gave 'ls' for the
> sake of an example. The real problem I am facing is, how to stay in
> the folder after popen('cd directory') finishes. It seems trivial, but
> I am not able to do it. 

The problem is that you are running 2 child, and it's the first
subprocess that's changing the directory and then exiting.  This
actually has little to do with Python specifically.  you can  see the
same thing if you do this:

$ pwd
/tmp
$ cat mycd.sh 
#!/bin/sh

cd /etc
$ ./mycd.sh 
$ pwd
/tmp

As you can see I am still in tmp.  This is because mycd.sh changed
to /etc/ but after it exits back to the parent process it is back
in /tmp.

What you want is to "cd" inside your script itself.  os.chdir() does
this.






More information about the Python-list mailing list