Index: Doc/library/subprocess.rst =================================================================== --- Doc/library/subprocess.rst (revision 76868) +++ Doc/library/subprocess.rst (working copy) @@ -157,7 +157,8 @@ The arguments are the same as for the Popen constructor. Example:: - retcode = call(["ls", "-l"]) + >>> import subprocess + >>> retcode = subprocess.call(["ls", "-l"]) .. warning:: @@ -176,7 +177,9 @@ The arguments are the same as for the Popen constructor. Example:: - check_call(["ls", "-l"]) + >>> import subprocess + >>> subprocess.check_call(["ls", "-l"]) + 0 .. warning:: @@ -194,16 +197,17 @@ The arguments are the same as for the :class:`Popen` constructor. Example:: + >>> import subprocess >>> subprocess.check_output(["ls", "-l", "/dev/null"]) - 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' + b'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' The stdout argument is not allowed as it is used internally. To capture standard error in the result, use ``stderr=subprocess.STDOUT``:: >>> subprocess.check_output( - ["/bin/sh", "-c", "ls non_existent_file ; exit 0"], - stderr=subprocess.STDOUT) - 'ls: non_existent_file: No such file or directory\n' + ... ["/bin/sh", "-c", "ls non_existent_file ; exit 0"], + ... stderr=subprocess.STDOUT) + b'ls: non_existent_file: No such file or directory\n' .. versionadded:: 3.1 Index: Lib/subprocess.py =================================================================== --- Lib/subprocess.py (revision 76868) +++ Lib/subprocess.py (working copy) @@ -110,7 +110,7 @@ The arguments are the same as for the Popen constructor. Example: - retcode = call(["ls", "-l"]) + >>> retcode = call(["ls", "-l"]) check_call(*popenargs, **kwargs): Run command with arguments. Wait for command to complete. If the @@ -120,45 +120,45 @@ The arguments are the same as for the Popen constructor. Example: - check_call(["ls", "-l"]) + >>> check_call(["ls", "-l"]) + 0 getstatusoutput(cmd): Return (status, output) of executing cmd in a shell. - Execute the string 'cmd' in a shell with os.popen() and return a 2-tuple - (status, output). cmd is actually run as '{ cmd ; } 2>&1', so that the - returned output will contain output or error messages. A trailing newline - is stripped from the output. The exit status for the command can be - interpreted according to the rules for the C function wait(). Example: + Execute the string 'cmd' in a shell with os.popen() and return a + 2-tuple (status, output). cmd is actually run as '{ cmd ; } 2>&1', + so that the returned output will contain output or error messages. + A trailing newline is stripped from the output. The exit status for + the command can be interpreted according to the rules for the C + function wait(). Example: - >>> import subprocess - >>> subprocess.getstatusoutput('ls /bin/ls') + >>> getstatusoutput('ls /bin/ls') (0, '/bin/ls') - >>> subprocess.getstatusoutput('cat /bin/junk') + >>> getstatusoutput('cat /bin/junk') (256, 'cat: /bin/junk: No such file or directory') - >>> subprocess.getstatusoutput('/bin/junk') + >>> getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') getoutput(cmd): Return output (stdout or stderr) of executing cmd in a shell. - Like getstatusoutput(), except the exit status is ignored and the return - value is a string containing the command's output. Example: + Like getstatusoutput(), except the exit status is ignored and the + return value is a string containing the command's output. Example: - >>> import subprocess - >>> subprocess.getoutput('ls /bin/ls') + >>> getoutput('ls /bin/ls') '/bin/ls' check_output(*popenargs, **kwargs): - Run command with arguments and return its output as a byte string. + Run command with arguments and return its output as a byte string. - If the exit code was non-zero it raises a CalledProcessError. The - CalledProcessError object will have the return code in the returncode - attribute and output in the output attribute. + If the exit code was non-zero it raises a CalledProcessError. The + CalledProcessError object will have the return code in the + returncode attribute and output in the output attribute. - The arguments are the same as for the Popen constructor. Example: + The arguments are the same as for the Popen constructor. Example: - output = subprocess.check_output(["ls", "-l", "/dev/null"]) + >>> output = check_output(["ls", "-l", "/dev/null"]) Exceptions @@ -438,24 +438,24 @@ def check_output(*popenargs, **kwargs): - """Run command with arguments and return its output as a byte string. + r"""Run command with arguments and return its output as a byte string. If the exit code was non-zero it raises a CalledProcessError. The - CalledProcessError object will have the return code in the returncode - attribute and output in the output attribute. + CalledProcessError object will have the return code in the + returncode attribute and output in the output attribute. The arguments are the same as for the Popen constructor. Example: >>> check_output(["ls", "-l", "/dev/null"]) - 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' + b'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' The stdout argument is not allowed as it is used internally. - To capture standard error in the result, use stderr=subprocess.STDOUT. + To capture standard error in the result, use stderr=STDOUT. >>> check_output(["/bin/sh", "-c", - "ls -l non_existent_file ; exit 0"], - stderr=subprocess.STDOUT) - 'ls: non_existent_file: No such file or directory\n' + ... "ls -l non_existent_file ; exit 0"], + ... stderr=STDOUT) + b'ls: non_existent_file: No such file or directory\n' """ if 'stdout' in kwargs: raise ValueError('stdout argument not allowed, it will be overridden.') @@ -547,11 +547,12 @@ def getstatusoutput(cmd): """Return (status, output) of executing cmd in a shell. - Execute the string 'cmd' in a shell with os.popen() and return a 2-tuple - (status, output). cmd is actually run as '{ cmd ; } 2>&1', so that the - returned output will contain output or error messages. A trailing newline - is stripped from the output. The exit status for the command can be - interpreted according to the rules for the C function wait(). Example: + Execute the string 'cmd' in a shell with os.popen() and return a + 2-tuple (status, output). cmd is actually run as '{ cmd ; } 2>&1', + so that the returned output will contain output or error messages. + A trailing newline is stripped from the output. The exit status for + the command can be interpreted according to the rules for the C + function wait(). Example: >>> import subprocess >>> subprocess.getstatusoutput('ls /bin/ls') @@ -572,8 +573,8 @@ def getoutput(cmd): """Return output (stdout or stderr) of executing cmd in a shell. - Like getstatusoutput(), except the exit status is ignored and the return - value is a string containing the command's output. Example: + Like getstatusoutput(), except the exit status is ignored and the + return value is a string containing the command's output. Example: >>> import subprocess >>> subprocess.getoutput('ls /bin/ls')