Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 470ee39

Browse files
committed
Issue #16115: Improve subprocess.Popen() documentation around args, shell, and executable arguments.
1 parent 0230b6a commit 470ee39

2 files changed

Lines changed: 38 additions & 30 deletions

File tree

Doc/library/subprocess.rst

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -290,20 +290,15 @@ functions.
290290

291291
Arguments are:
292292

293-
*args* should be a string, or a sequence of program arguments. The program
294-
to execute is normally the first item in the args sequence or the string if
295-
a string is given, but can be explicitly set by using the *executable*
296-
argument. When *executable* is given, the first item in the args sequence
297-
is still treated by most programs as the command name, which can then be
298-
different from the actual executable name. On Unix, it becomes the display
299-
name for the executing program in utilities such as :program:`ps`.
300-
301-
On Unix, with *shell=False* (default): In this case, the Popen class uses
302-
:meth:`os.execvp` like behavior to execute the child program.
303-
*args* should normally be a
304-
sequence. If a string is specified for *args*, it will be used as the name
305-
or path of the program to execute; this will only work if the program is
306-
being given no arguments.
293+
*args* should be a sequence of program arguments or else a single string.
294+
By default, the program to execute is the first item in *args* if *args* is
295+
a sequence and the string itself if *args* is a string. However, see the
296+
*shell* and *executable* arguments for differences from this behavior.
297+
298+
On Unix, the :class:`Popen` class uses :meth:`os.execvp`-like behavior to
299+
execute the child program. If *args* is a string, the string is
300+
interpreted as the name or path of the program to execute; this only works
301+
if the program is being given no arguments.
307302

308303
.. note::
309304

@@ -324,13 +319,23 @@ functions.
324319
used in the shell (such as filenames containing spaces or the *echo* command
325320
shown above) are single list elements.
326321

327-
On Unix, with *shell=True*: If args is a string, it specifies the command
328-
string to execute through the shell. This means that the string must be
322+
On Windows, the :class:`Popen` class uses ``CreateProcess()`` to
323+
execute the child program, which operates on strings. If *args* is a
324+
sequence, it will be converted to a string in a manner described in
325+
:ref:`converting-argument-sequence`.
326+
327+
The *shell* argument (which defaults to *False*) specifies whether to use
328+
the shell as the program to execute. It is recommended to pass *args* as a
329+
sequence if *shell* is *False* and as a string if *shell* is *True*.
330+
331+
On Unix with ``shell=True``, the shell defaults to :file:`/bin/sh`. If
332+
*args* is a string, the string specifies the command
333+
to execute through the shell. This means that the string must be
329334
formatted exactly as it would be when typed at the shell prompt. This
330335
includes, for example, quoting or backslash escaping filenames with spaces in
331336
them. If *args* is a sequence, the first item specifies the command string, and
332337
any additional items will be treated as additional arguments to the shell
333-
itself. That is to say, *Popen* does the equivalent of::
338+
itself. That is to say, :class:`Popen` does the equivalent of::
334339

335340
Popen(['/bin/sh', '-c', args[0], args[1], ...])
336341

@@ -340,10 +345,11 @@ functions.
340345
input. See the warning under :ref:`frequently-used-arguments`
341346
for details.
342347

343-
On Windows: the :class:`Popen` class uses CreateProcess() to execute the
344-
child program, which operates on strings. If *args* is a sequence, it will
345-
be converted to a string in a manner described in
346-
:ref:`converting-argument-sequence`.
348+
On Windows with ``shell=True``, the :envvar:`COMSPEC` environment variable
349+
specifies the default shell. The only time you need to specify
350+
``shell=True`` on Windows is when the command you wish to execute is built
351+
into the shell (e.g. :command:`dir` or :command:`copy`). You do not need
352+
``shell=True`` to run a batch file or console-based executable.
347353

348354
*bufsize*, if given, has the same meaning as the corresponding argument to the
349355
built-in open() function: :const:`0` means unbuffered, :const:`1` means line
@@ -357,15 +363,14 @@ functions.
357363
enable buffering by setting *bufsize* to either -1 or a large enough
358364
positive value (such as 4096).
359365

360-
The *executable* argument specifies the program to execute. It is very seldom
361-
needed: Usually, the program to execute is defined by the *args* argument. If
362-
``shell=True``, the *executable* argument specifies which shell to use. On Unix,
363-
the default shell is :file:`/bin/sh`. On Windows, the default shell is
364-
specified by the :envvar:`COMSPEC` environment variable. The only reason you
365-
would need to specify ``shell=True`` on Windows is where the command you
366-
wish to execute is actually built in to the shell, eg ``dir``, ``copy``.
367-
You don't need ``shell=True`` to run a batch file, nor to run a console-based
368-
executable.
366+
The *executable* argument specifies a replacement program to execute. It
367+
is very seldom needed. When ``shell=False``, *executable* replaces the
368+
program to execute specified by *args*. However, the *args* program is
369+
still treated by most programs as the command name, which can then be
370+
different from the program actually executed. On Unix, the *args* name
371+
becomes the display name for the executable in utilities such as
372+
:program:`ps`. If ``shell=True``, on Unix the *executable* argument
373+
specifies a replacement shell for the default :file:`/bin/sh`.
369374

370375
*stdin*, *stdout* and *stderr* specify the executed program's standard input,
371376
standard output and standard error file handles, respectively. Valid values

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ Build
609609
Documentation
610610
-------------
611611

612+
- Issue #16115: Improve subprocess.Popen() documentation around args, shell,
613+
and executable arguments.
614+
612615
- Issue #13498: Clarify docs of os.makedirs()'s exist_ok argument. Done with
613616
great native-speaker help from R. David Murray.
614617

0 commit comments

Comments
 (0)