2

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?

1
  • 1
    Is there a reason there is a different number of a's in each ctaa...aags? Commented Mar 7, 2014 at 13:48

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

subprocess.Popen(['ctaaags', '--help'], shell=True)

2 Comments

...but encouraging people to use shell=True is bad practice; in general, it introduces room for bugs, security-impacting and otherwise, as things intended to be program parameters can be (mis)interpreted by the shell.
don't use a list argument with shell=True. It does completely different thing on Unix. You don't need shell=True on Windows unless you want to a run a builtin command.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.