os 모듈과 subprocess 모듈을 통해 쉘 명령을 실행하면 하나는 작동하고 다른 하나는 작동하지 않습니다.

os 모듈과 subprocess 모듈을 통해 쉘 명령을 실행하면 하나는 작동하고 다른 하나는 작동하지 않습니다.

os 모듈과 subprocess 모듈을 통해 쉘 명령을 실행하는 방법을 배우고 있습니다. 아래는 내 코드입니다.

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')

두 번째는 완벽하게 작동하지만 첫 번째는 작동하지 않고 다음과 같은 오류가 발생합니다.

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

두 방법 모두 동일한 효과가 있다고 생각합니다. 여기서 내가 틀릴 수 있는 점을 지적해 주실 수 있나요? 매우 감사합니다.

답변1

둘 사이의 한 가지 차이점이 문서화되어 있습니다(여기)

os.system(명령)

서브쉘에서 명령(문자열)을 실행합니다.

하지만subprocess.call()좋다:

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

args에 설명된 명령을 실행합니다. 명령이 완료될 때까지 기다린 후 returncode 속성을 반환합니다.

동작을 통과해야 하는 것과 동일하게 만들려면 subprocess.call(). 그래서 이렇게 :os.system()shell=True

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

관련 정보