blob: a700628c79d850f90140fd579d0be9b7b308e33f [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`commands` --- Utilities for running commands
3==================================================
4
5.. module:: commands
6 :platform: Unix
7 :synopsis: Utility functions for running external commands.
Georg Brandl208b00d2009-04-25 15:11:29 +00008 :deprecated:
9
10.. deprecated:: 2.6
11 The :mod:`commands` module has been removed in Python 3.0. Use the
12 :mod:`subprocess` module instead.
13
Georg Brandl8ec7f652007-08-15 14:28:01 +000014.. sectionauthor:: Sue Williams <sbw@provis.com>
15
16
17The :mod:`commands` module contains wrapper functions for :func:`os.popen` which
18take a system command as a string and return any output generated by the command
19and, optionally, the exit status.
20
21The :mod:`subprocess` module provides more powerful facilities for spawning new
22processes and retrieving their results. Using the :mod:`subprocess` module is
23preferable to using the :mod:`commands` module.
24
Benjamin Peterson9171bed2008-05-26 20:41:45 +000025.. warning::
26
27 In 3.x, :func:`getstatus` and two undocumented functions (:func:`mk2arg` and
28 :func:`mkarg`) have been removed. Also, :func:`getstatusoutput` and
29 :func:`getoutput` have been moved to the :mod:`subprocess` module.
30
Georg Brandl8ec7f652007-08-15 14:28:01 +000031The :mod:`commands` module defines the following functions:
32
33
34.. function:: getstatusoutput(cmd)
35
36 Execute the string *cmd* in a shell with :func:`os.popen` and return a 2-tuple
37 ``(status, output)``. *cmd* is actually run as ``{ cmd ; } 2>&1``, so that the
38 returned output will contain output or error messages. A trailing newline is
39 stripped from the output. The exit status for the command can be interpreted
40 according to the rules for the C function :cfunc:`wait`.
41
42
43.. function:: getoutput(cmd)
44
45 Like :func:`getstatusoutput`, except the exit status is ignored and the return
46 value is a string containing the command's output.
47
48
49.. function:: getstatus(file)
50
51 Return the output of ``ls -ld file`` as a string. This function uses the
52 :func:`getoutput` function, and properly escapes backslashes and dollar signs in
53 the argument.
54
55 .. deprecated:: 2.6
Andrew M. Kuchlingf839b662008-06-20 23:14:32 +000056 This function is nonobvious and useless. The name is also misleading in the
Benjamin Peterson9171bed2008-05-26 20:41:45 +000057 presence of :func:`getstatusoutput`.
Benjamin Peterson3aa84a72008-05-26 19:41:53 +000058
Georg Brandl8ec7f652007-08-15 14:28:01 +000059
60Example::
61
62 >>> import commands
63 >>> commands.getstatusoutput('ls /bin/ls')
64 (0, '/bin/ls')
65 >>> commands.getstatusoutput('cat /bin/junk')
66 (256, 'cat: /bin/junk: No such file or directory')
67 >>> commands.getstatusoutput('/bin/junk')
68 (256, 'sh: /bin/junk: not found')
69 >>> commands.getoutput('ls /bin/ls')
70 '/bin/ls'
71 >>> commands.getstatus('/bin/ls')
72 '-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
73
74
75.. seealso::
76
77 Module :mod:`subprocess`
78 Module for spawning and managing subprocesses.
79