sandbox/morph / Doc / library / commands.rst

:mod:`commands` --- Utilities for running commands

The :mod:`commands` module contains wrapper functions for :func:`os.popen` which take a system command as a string and return any output generated by the command and, optionally, the exit status.

The :mod:`subprocess` module provides more powerful facilities for spawning new processes and retrieving their results. Using the :mod:`subprocess` module is preferable to using the :mod:`commands` module.


In Python 3.x, :func:`getstatus` and two undocumented functions (:func:`mk2arg` and :func:`mkarg`) have been removed. Also, :func:`getstatusoutput` and :func:`getoutput` have been moved to the :mod:`subprocess` module.

The :mod:`commands` module defines the following functions:


>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x  1 root        13352 Oct 14  1994 /bin/ls'