ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH

Reference
ActivePython 2.5
Python Documentation
Library Reference
16. Unix Specific Services
16.1 posix -- The most common POSIX system calls
16.2 pwd -- The password database
16.3 spwd -- The shadow password database
16.4 grp -- The group database
16.5 crypt -- Function to check Unix passwords
16.6 dl -- Call C functions in shared objects
16.7 termios -- POSIX style tty control
16.8 tty -- Terminal control functions
16.9 pty -- Pseudo-terminal utilities
16.10 fcntl -- The fcntl() and ioctl() system calls
16.11 pipes -- Interface to shell pipelines
16.12 posixfile -- File-like objects with locking support
16.13 resource -- Resource usage information
16.14 nis -- Interface to Sun's NIS (Yellow Pages)
16.15 syslog -- Unix syslog library routines
16.16 commands -- Utilities for running commands

MyASPN >> Reference >> ActivePython 2.5 >> Python Documentation >> Library Reference >> 16. Unix Specific Services
ActivePython 2.5 documentation

16.16 commands -- Utilities for running commands

Availability: Unix.

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

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

The commands module defines the following functions:

getstatusoutput( cmd)
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().

getoutput( cmd)
Like getstatusoutput(), except the exit status is ignored and the return value is a string containing the command's output.

getstatus( file)
Return the output of "ls -ld file" as a string. This function uses the getoutput() function, and properly escapes backslashes and dollar signs in the argument.

Example:

>>> 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')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x  1 root        13352 Oct 14  1994 /bin/ls'

See Also:

Module subprocess:
Module for spawning and managing subprocesses.

See About this document... for information on suggesting changes.

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved