subprocess.call using string vs using list
subprocess
's rules for handling the command argument are actually a bit complex.
From the docs:
args
should be a sequence of program arguments or else a single string. By default, the program to execute is the first item inargs
ifargs
is a sequence. Ifargs
is a string, the interpretation is platform-dependent and described below. See theshell
andexecutable
arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to passargs
as a sequence.... Ifshell
is True, it is recommended to passargs
as a string rather than as a sequence.
With shell=False
:
On Unix, if
args
is a string, the string is interpreted as the name or path of the program to execute. However, this can only be done if not passing arguments to the program.On Windows, if
args
is a sequence, it will be converted to a string in a manner described in Converting an argument sequence to a string on Windows. This is because the underlyingCreateProcess()
operates on strings.
With shell=True
:
On Unix with
shell=True
, the shell defaults to/bin/sh
. Ifargs
is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself.On Windows with
shell=True
, theCOMSPEC
environment variable specifies the default shell. The only time you need to specifyshell=True
on Windows is when the command you wish to execute is built into the shell (e.g.dir
orcopy
). You do not needshell=True
to run a batch file or console-based executable.
(all emphasis mine)