Added section on passing contextual information to logging and documentation for...
[python.git] / Doc / library / commands.rst
blob177710b5676d5b183ae57de89792ac7742aec96d
2 :mod:`commands` --- Utilities for running commands
3 ==================================================
5 .. module:: commands
6    :platform: Unix
7    :synopsis: Utility functions for running external commands.
8 .. sectionauthor:: Sue Williams <sbw@provis.com>
11 The :mod:`commands` module contains wrapper functions for :func:`os.popen` which
12 take a system command as a string and return any output generated by the command
13 and, optionally, the exit status.
15 The :mod:`subprocess` module provides more powerful facilities for spawning new
16 processes and retrieving their results.  Using the :mod:`subprocess` module is
17 preferable to using the :mod:`commands` module.
19 The :mod:`commands` module defines the following functions:
22 .. function:: getstatusoutput(cmd)
24    Execute the string *cmd* in a shell with :func:`os.popen` and return a 2-tuple
25    ``(status, output)``.  *cmd* is actually run as ``{ cmd ; } 2>&1``, so that the
26    returned output will contain output or error messages. A trailing newline is
27    stripped from the output. The exit status for the command can be interpreted
28    according to the rules for the C function :cfunc:`wait`.
31 .. function:: getoutput(cmd)
33    Like :func:`getstatusoutput`, except the exit status is ignored and the return
34    value is a string containing the command's output.
37 .. function:: getstatus(file)
39    Return the output of ``ls -ld file`` as a string.  This function uses the
40    :func:`getoutput` function, and properly escapes backslashes and dollar signs in
41    the argument.
43    .. deprecated:: 2.6
44       This function is nonobvious and useless, also the name is misleading in the
45       presence of :func:`getstatusoutput`.
47 Example::
49    >>> import commands
50    >>> commands.getstatusoutput('ls /bin/ls')
51    (0, '/bin/ls')
52    >>> commands.getstatusoutput('cat /bin/junk')
53    (256, 'cat: /bin/junk: No such file or directory')
54    >>> commands.getstatusoutput('/bin/junk')
55    (256, 'sh: /bin/junk: not found')
56    >>> commands.getoutput('ls /bin/ls')
57    '/bin/ls'
58    >>> commands.getstatus('/bin/ls')
59    '-rwxr-xr-x  1 root        13352 Oct 14  1994 /bin/ls'
62 .. seealso::
64    Module :mod:`subprocess`
65       Module for spawning and managing subprocesses.