| 1 | """Execute shell commands via os.popen() and return status, output.
|
|---|
| 2 |
|
|---|
| 3 | Interface summary:
|
|---|
| 4 |
|
|---|
| 5 | import commands
|
|---|
| 6 |
|
|---|
| 7 | outtext = commands.getoutput(cmd)
|
|---|
| 8 | (exitstatus, outtext) = commands.getstatusoutput(cmd)
|
|---|
| 9 | outtext = commands.getstatus(file) # returns output of "ls -ld file"
|
|---|
| 10 |
|
|---|
| 11 | A trailing newline is removed from the output string.
|
|---|
| 12 |
|
|---|
| 13 | Encapsulates the basic operation:
|
|---|
| 14 |
|
|---|
| 15 | pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
|
|---|
| 16 | text = pipe.read()
|
|---|
| 17 | sts = pipe.close()
|
|---|
| 18 |
|
|---|
| 19 | [Note: it would be nice to add functions to interpret the exit status.]
|
|---|
| 20 | """
|
|---|
| 21 |
|
|---|
| 22 | __all__ = ["getstatusoutput","getoutput","getstatus"]
|
|---|
| 23 |
|
|---|
| 24 | # Module 'commands'
|
|---|
| 25 | #
|
|---|
| 26 | # Various tools for executing commands and looking at their output and status.
|
|---|
| 27 | #
|
|---|
| 28 | # NB This only works (and is only relevant) for UNIX.
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | # Get 'ls -l' status for an object into a string
|
|---|
| 32 | #
|
|---|
| 33 | def getstatus(file):
|
|---|
|
|---|