문제

If I run the following command from the cmd.exe, I get an error message that looks like so:

C:\Users\user>ctaags --help
'ctaags' is not recognized as an internal or external command,
operable program or batch file.

Now if I run the same command using Python's subprocess module, I get a different warning:

>>> subprocess.Popen(['ctaags', '--help'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "c:\Python27\lib\subprocess.py", line 948, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

Based on other commands, I'd expect the output from Popen to exactly match what the shell gives me back, i.e.:

WindowsError: [Error 2] 'ctaags' is not recognized as an internal or
external command, operable program or batch file.

This is not the case. Why?

도움이 되었습니까?

해결책

The syscall to run the process returns an error 2 regardless of whether it's called by the shell or from Python.

In the shell case, it's the shell that then generates the "not recognized" message.

Using subprocess.Popen() without shell=True (which you should not use without necessity), no shell is present, so you get the raw error code back yourself.

다른 팁

Popen does not execute the command using the shell unless you set shell=True:

subprocess.Popen(['ctaaags', '--help'], shell=True)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top