1

The docs for the subprocess module state that 'If shell is True, the specified command will be executed through the shell'. What does this mean in practice, on a Windows OS?

4 Answers 4

3

It means that the command will be executed using the program specified in the COMSPEC environment variable. Usually cmd.exe.

To be exact, subprocess calls the CreateProcess windows api function, passing "cmd.exe /c " + args as the lpCommandLine argument.

If shell==False, the lpCommandLine argument to CreateProcess is simply args.

1

When you execute an external process, the command you want may look something like "foo arg1 arg2 arg3". If "foo" is an executable, that is what gets executed and given the arguments.

However, often it is the case that "foo" is actually a script of some sort, or maybe a command that is built-in to the shell and not an actual executable file on disk. In this case the system can't execute "foo" directly because, strictly speaking, these sorts of things aren't executable. They need some sort of "shell" to execute them. On *nix systems this shell is typically (but not necessarily) /bin/sh. On windows it will typically be cmd.exe (or whatever is stored in the COMSPEC environment variable).

This parameter lets you define what shell you wish to use to execute your command, for the relatively rare case when you don't want the default.

0

In addition to what was said in other answers, it is useful in practice if you want to open a file in the default viewer for that file type. For instance, if you want to open an HTML or PDF file, but will not know which browser or viewer is installed on the systems it will be run on, or have no guarantees as to the path to the executable, you can simply pass the file name as the only argument for the args field, then set shell=True. This will have Windows use whatever program is associated with that file type. One caveat, if the path to your file has spaces, you need to surround it with two ".

eg.

path = "C:\\Documents and Settings\\Bob\\Desktop\\New Folder\\README.txt"
subprocess.call('""' + path + '""', shell = True)
2
  • 1
    os.startfile is a better method for doing this.
    – Brian
    Apr 21, 2009 at 16:31
  • Wow. I didn't know that even existed. I dug around for hours on Google and this was the best I could find. Apr 21, 2009 at 17:20
0

In using-the-subprocess-module, there is an explicit paragraph:

The executable argument specifies the program to execute. It is very seldom needed: Usually, the program to execute is defined by the args argument. If shell=True, the executable argument specifies which shell to use. On Unix, the default shell is /bin/sh. On Windows, the default shell is specified by the COMSPEC environment variable.

Windows example - the shell (cmd.exe) command date -t will not be recognized without the shell:

>>> p=subprocess.Popen(["date", "/t"], stdout=subprocess.PIPE)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python26\lib\subprocess.py", line 595, in __init__
    errread, errwrite)
  File "C:\Python26\lib\subprocess.py", line 804, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>> 

Using a shell, all is well:

>>> p=subprocess.Popen(["date", "/t"], shell=True, stdout=subprocess.PIPE)
>>> p.communicate()
('Wed 04/22/2009 \r\n', None)
>>>
2
  • I think the OP is looking for more than just a regurgitation of the documentation. I think they don't grok the whole concept of a shell in this context and are looking for a little more than what the documentation gives. Apr 21, 2009 at 14:09
  • @BryanOakley - I disagree. This is exactly what I needed. I was looking through the documentation and was unable to find the section which went into detail on what shell=True or shell=False does until I found gimel's answer here which pulled it out and emphasized different parts. Jul 8, 2015 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.