Is subprocess.Popen completely broken?
Jerry Hill
malaclypse2 at gmail.com
Thu Mar 27 11:05:19 EDT 2008
On Thu, Mar 27, 2008 at 10:53 AM, Skip Montanaro <skip at pobox.com> wrote:
> I am trying to replace os.system calls with subprocess.Popen. This simple
> example fails miserably:
>
> >>> proc = subprocess.Popen ("ls /tmp")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/home/titan/skipm/local/lib/python2.5/subprocess.py", line 594, in __init__
> errread, errwrite)
> File "/home/titan/skipm/local/lib/python2.5/subprocess.py", line 1091, in
> _execute_child
> raise child_exception
> OSError: [Errno 2] No such file or directory
>
> I also tried explicitly referencing /usr/bin/ls. Same result. What gives?
It's looking for an executable named "ls /tmp" Since it can't find
one, it raises an exception.
If you just want to replace an os.system call, you need to pass
shell=True to Popen, like this:
proc = subprocess.Popen("ls /tmp", shell=True)
That will get the shell to split your string into the program to be
called, and the argument(s) to the program. Alternatively, you can do
it yourself by passing a sequence to Popen:
proc = subprocess.Popen(["ls", "/tmp"])
Take a look at the documentation for Popen
(http://docs.python.org/lib/node528.html) and the specific examples
for replacing os.system (http://docs.python.org/lib/node536.html)
--
Jerry
More information about the Python-list
mailing list