[Tutor] using python to execute from Dir A in Dir B
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,
Author of the Learn to Program web site
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor