Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this command work with os.system() but not subprocess.Popen()?

I want to delete a number of jobs from a q. The command to delete the job is qdel JOBid.

Initially, I tried to use the subprocess module, but I got an error: #!/usr/bin/env python

 import sys, os, subprocess as sp

 lo = sys.argv[1]
 hi = sys.argv[2]

 lo = int(lo)
 hi = int(hi)


for i in range(lo,hi):
    print "i is %d"%i
    p=sp.Popen(['qdel %d'%i],stdout=sp.PIPE)
    #os.system('qdel %d'%i)

So this did not work. The error I got was

Traceback (most recent call last):
File "del.py", line 14, in <module>
p=sp.Popen(['qdel %d'%i],stdout=sp.PIPE)
File "/usr/lib64/python2.6/subprocess.py", line 639, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Then I commented out the subprocess line and used os and it worked immediately. I think I don't fully understand the subprocess module

#!/usr/bin/env python

import sys, os, subprocess as sp

lo = sys.argv[1]
hi = sys.argv[2]

lo = int(lo)
hi = int(hi)


    for i in range(lo,hi):
         print "i is %d"%i
         #p=sp.Popen(['qdel %d'%i],stdout=sp.PIPE)
         os.system('qdel %d'%i)

The above code worked flawlessly. I just want to know why and what the advantages are of the subprocess module. Also, I am using a unix shell

like image 846
ironcyclone Avatar asked Jun 21 '12 16:06

ironcyclone


People also ask

What is the difference between subprocess Popen and os system?

os. system is equivalent to Unix system command, while subprocess was a helper module created to provide many of the facilities provided by the Popen commands with an easier and controllable interface. Those were designed similar to the Unix Popen command.

How does subprocess Popen work?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

What does os Popen do in Python?

Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.

What is difference between subprocess Popen and call?

Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.


2 Answers

If you read manual, you can see that your call to Popen is wrong: you should pass not a single command, but an array of arguments:

p=sp.Popen(['qdel', '%d'%i],stdout=sp.PIPE)

Alternatively, as sc0tt's answer points out, you can use shell=True, but this has some disadvantages in more complex cases: you would have to manually escape all the variable data in the command in case it contains, for example, filenames with spaces or anything much more potentially harmful (like ;)

like image 170
aland Avatar answered Nov 05 '22 18:11

aland


You want to use shell=True on your Popen call.

p=sp.Popen(['qdel %d'%i], shell=True, stdout=sp.PIPE)
like image 40
smont Avatar answered Nov 05 '22 16:11

smont