1

I'm learning how to run shell commands through the os module and the subprocess module. The following is my code.

from subprocess import call
call('/usr/lib/mailman/bin/find_member -w user_email')
import os
os.system('/usr/lib/mailman/bin/find_member -w user_email')

The second one works very well, while on the other hand, the first one does not work, and I got the following errors.

Traceback (most recent call last):
  File "fabfile.py", line 6, in <module>
    call('/usr/lib/mailman/bin/find_member -w user_email')
  File "/usr/lib64/python2.6/subprocess.py", line 478, in call
    p = Popen(*popenargs, **kwargs)
  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

I thought those two methods have the same effect. Could you point to me what's the possible errors here? Many thanks.

  • Try: call('/usr/lib/mailman/bin/find_member -w user_email', shell=True) or, better yet: call(['/usr/lib/mailman/bin/find_member', '-w', 'user_email']). – John1024 Jun 12 '17 at 20:21
2

One difference between the two is documented (Here)

os.system(command)

Execute the command (a string) in a subshell.

While subprocess.call() looks like:

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

Run the command described by args. Wait for command to complete, then return the returncode attribute.

To make subprocess.call() behave the same as os.system() you need to pass shell=True. So something like:

from subprocess import call
call('/usr/lib/mailman/bin/find_member -w user_email', shell=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.