subprocess‘s rules for handling the command argument are actually a bit complex.
Generally speaking, to run external commands, you should use shell=False and pass the arguments as a sequence. Use shell=True only if you need to use shell built-in commands or specific shell syntax; using shell=True correctly is platform-specific as detailed below.
From the docs:
argsshould be a sequence of program arguments or else a single string. By default, the program to execute is the first item inargsifargsis a sequence. Ifargsis a string, the interpretation is platform-dependent and described below. See theshellandexecutablearguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to passargsas a sequence…. Ifshellis True, it is recommended to passargsas a string rather than as a sequence.
With shell=False:
On Unix, if
argsis 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
argsis 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. Ifargsis 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, theCOMSPECenvironment variable specifies the default shell. The only time you need to specifyshell=Trueon Windows is when the command you wish to execute is built into the shell (e.g.dirorcopy). You do not needshell=Trueto run a batch file or console-based executable.
(all emphasis mine)