~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Doc/library/subprocess.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
 
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).
53
 
 
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
57
 
   arguments.
 
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.
 
54
 
 
55
   .. note::
 
56
 
 
57
      :meth:`shlex.split` can be useful when determining the correct
 
58
      tokenization for *args*, especially in complex cases::
 
59
 
 
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)
 
64
         >>> print args
 
65
         ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
 
66
         >>> p = subprocess.Popen(args) # Success!
 
67
 
 
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.
 
73
 
 
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::
 
81
 
 
82
      Popen(['/bin/sh', '-c', args[0], args[1], ...])
58
83
 
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
 
105
   executable.
77
106
 
78
107
   *stdin*, *stdout* and *stderr* specify the executed programs' standard input,
79
108
   standard output and standard error file handles, respectively.  Valid values
365
394
   sts = os.system("mycmd" + " myarg")
366
395
   ==>
367
396
   p = Popen("mycmd" + " myarg", shell=True)
368
 
   sts = os.waitpid(p.pid, 0)
 
397
   sts = os.waitpid(p.pid, 0)[1]
369
398
 
370
399
Notes:
371
400