49
49
On Unix, with *shell=False* (default): In this case, the Popen class uses
50
50
:meth:`os.execvp` to execute the child program. *args* should normally be a
51
sequence. A string will be treated as a sequence with the string as the only
52
item (the program to execute).
54
On Unix, with *shell=True*: If args is a string, it specifies the command string
55
to execute through the shell. If *args* is a sequence, the first item specifies
56
the command string, and any additional items will be treated as additional shell
51
sequence. If a string is specified for *args*, it will be used as the name
52
or path of the program to execute; this will only work if the program is
53
being given no arguments.
57
:meth:`shlex.split` can be useful when determining the correct
58
tokenization for *args*, especially in complex cases::
60
>>> import shlex, subprocess
61
>>> command_line = raw_input()
62
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
63
>>> args = shlex.split(command_line)
65
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
66
>>> p = subprocess.Popen(args) # Success!
68
Note in particular that options (such as *-input*) and arguments (such
69
as *eggs.txt*) that are separated by whitespace in the shell go in separate
70
list elements, while arguments that need quoting or backslash escaping when
71
used in the shell (such as filenames containing spaces or the *echo* command
72
shown above) are single list elements.
74
On Unix, with *shell=True*: If args is a string, it specifies the command
75
string to execute through the shell. This means that the string must be
76
formatted exactly as it would be when typed at the shell prompt. This
77
includes, for example, quoting or backslash escaping filenames with spaces in
78
them. If *args* is a sequence, the first item specifies the command string, and
79
any additional items will be treated as additional arguments to the shell
80
itself. That is to say, *Popen* does the equivalent of::
82
Popen(['/bin/sh', '-c', args[0], args[1], ...])
59
84
On Windows: the :class:`Popen` class uses CreateProcess() to execute the child
60
85
program, which operates on strings. If *args* is a sequence, it will be
73
98
needed: Usually, the program to execute is defined by the *args* argument. If
74
99
``shell=True``, the *executable* argument specifies which shell to use. On Unix,
75
100
the default shell is :file:`/bin/sh`. On Windows, the default shell is
76
specified by the :envvar:`COMSPEC` environment variable.
101
specified by the :envvar:`COMSPEC` environment variable. The only reason you
102
would need to specify ``shell=True`` on Windows is where the command you
103
wish to execute is actually built in to the shell, eg ``dir``, ``copy``.
104
You don't need ``shell=True`` to run a batch file, nor to run a console-based
78
107
*stdin*, *stdout* and *stderr* specify the executed programs' standard input,
79
108
standard output and standard error file handles, respectively. Valid values