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

I want to delete several jobs from q. Command to delete a task qdel JOBid.

At first 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 it didn’t work. The error I received 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 do not 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 are the benefits of the subprocess module. I also use the unix shell

+5
source share
2 answers

, , Popen : , :

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

, sc0tt answer, shell=True, : , , , - (, ;)

+3

shell = True Popen.

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

All Articles