A recent requirement is to execute the shell command on the page, the first thought is Os.system,
| The code is as follows |
Copy Code |
| Os.system (' Cat/proc/cpuinfo ') |
But the results of a printed command on the page 0 or 1, of course, do not meet the requirements.
Try the second scenario Os.popen ()
| The code is as follows |
Copy Code |
Output = Os.popen (' Cat/proc/cpuinfo ') Print Output.read () |
A file read object is returned by Os.popen () that reads the read () action to see the output being executed. However, the return value performed by the program cannot be read www.111cn.net
Try a third scheme commands.getstatusoutput () a method can get to the return value and output, very useful.
| The code is as follows |
Copy Code |
(status, Output) = Commands.getstatusoutput (' Cat/proc/cpuinfo ') Print status, output |
An example given in the Python Document,
| The code is as follows |
Copy Code |
>>> 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 1994/bin/ls ' |
The results of the command execution can also be displayed on the last page based on the return value.