1. Use Os.system (cmd) but not the return value
2. Use Os.popen (cmd) to get the output of the command, just call the next read () or readlines (), such as A=os.popen (CMD). Read ()
3. Use the commands module. In fact, it is also the encapsulation of Popen. This module mainly has the following methods
-
- commands. Getstatusoutput (
cmd) returns (status, output).
-
- commands. GetOutput (
cmd) returns only output results
-
- commands. GetStatus (
file) returns the execution result string of the LS-ld file, calls the getoutput, and does not recommend using this method .
-
- Such as
-
>>>Importcommands>>> 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'
python--three ways to invoke shell commands