[Tutor] using python to execute from Dir A in Dir B

Alan Gauld alan.gauld at btinternet.com
Fri Oct 5 09:17:37 CEST 2007

"Andre Walker-Loud" <walksloud at gmail.com> wrote

> this module.class - if someone with more experience would feel  
> inclined to provide an example, I would be very much appreciative.

The subprocess module documentation has several examples 
of using both the Popen class and the call function.

The given example for os.system is:

sts = os.system("mycmd" + " myarg")
p = Popen("mycmd" + " myarg", shell=True)
sts = os.waitpid(p.pid, 0)To which you would need to add cwd=path making it:p = Popen("mycmd" + " myarg", shell=True, cwd=path)
sts = os.waitpid(p.pid, 0)The other difference using Popen is that it raises an OSError exception if the program fails to run so you should wrap the calls in a try/except if you want to catch that.HTH,

Alan Gauld
Author of the Learn to Program web site
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071005/249f2f2a/attachment.htm 

More information about the Tutor mailing list