1.os.system ()
Advantages: Simple, linux&widnows and other platforms are available, just to determine whether the return result is 0 or 1 to determine if the execution is successful.
Disadvantage: Unable to get return output.
Example:
Os.system (' ls ')
2.os.popen ()
Pros: can get output results
Disadvantage: Unable to get execution result, need to make judgment processing according to output result
Example:
Output = Os.popen (' ls ') print output.read ()
3.commands.getstatusoutput ()
Pros: Can simultaneously get execution results and return results
Cons: Windows platform does not support
Example:
Status, Output = Commands.getstatusoutput (' dir ') print status, output
4.subprocess. Popen ()
Advantage: can get execution result and return result simultaneously, support Linux&windows platform
Example:
p = subprocess. Popen (cmd, shell=true, stdin=subprocess. PIPE, Stdout=subprocess. PIPE, Stderr=subprocess. PIPE) msg = "For line in P.stdout.readlines (): msg + = Linestatus = p.wait () output = msg
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Several ways that Python executes shell directives