Using Python to invoke External system commands can improve coding efficiency. When the External System command is called, it can be further processed by getting the command to execute the return result code and execute the output. This article mainly describes Python's common methods of invoking external system commands, including Os.system (), Os.popen (), and subprocess. Popen () and so on.
1. subprocess Module
The subprocess module is preferred because the module can replace the old module methods, such as Os.system (), Os.popen (), etc., recommended to use. The Subporcess module can invoke External system commands to create a new child process, connect to the Nput/output/error pipeline of the child process, and get the return value of the child process. Subprocess modules are mainly call (), Check_call (), Check_output (), Popen () functions, briefly described as follows:
Main API ======== for return code. if return is not 0 return Code class for in a new process
Here's how to use the subprocess function.
Subprocess. Popen class
Constructor: Subprocess. Popen (args, Stdin=none, Stdout=none, Stderr=none, Shell=false) #参数太多, listing only key parameters
Description
(1) The args parameter is the External System command to invoke.
(2) When the shell parameter value is false, the corresponding program is executed on Linux by calling OS.EXECVP. When Trule, the system shell is called directly on Linux to execute the program. On windows, whether the shell is false or true, it is called CreateProcess to execute the specified external program.
(3) stdin, stdout, stdout are used to specify the standard input, standard output, and standard error of the subprocess, respectively. The value can be pipe, file descriptor, and none. The default value is None.
Examples of usage are as follows:
Import= subprocess. Popen (args='ping-n 2-w 3 192.168.1.104', stdin=subprocess. PIPE, Stdout=subprocess. Pipe,stderr=subprocess. PIPE, shell=True) p.wait ()print p.stdout.read ()
Description: After obtaining the output, P.stdout (<open file ' <fdopen> ', Mode ' RB ' >) becomes a readable file object that can be read using the file manipulation function.
Subprocess.call ()
function Prototypes:call(*popenargs, **kwargs). Call () invokes the External System command execution and returns the program execution result code.
Import= Subprocess.call ('ping-n 2-w 3 192.168.1.104', shell=True) Print Retcode
Subprocess.check_call ()
Use the same method as call (). If the call command executes successfully, the result code 0 is returned, and if execution fails, the Calledprocesserror. Exception is thrown. Examples are as follows:
>>> p = Subprocess.check_call ('ping-n 2-w 3 192.168.1.105', shell=True) is pinging192.168.1.105 has 32bytes of data: Request timed out. The request timed out. 192.168.1.105Ping Statistics: packets: Sent= 2, received = 0, missing = 2 (100%lost), Traceback (most recent call last): File"<stdin>", Line 1,inch<module>File"c:\Python27\lib\subprocess.py", line 186,inchCheck_callRaisecalledprocesserror (retcode, cmd) subprocess. Calledprocesserror:command'ping-n 2-w 3 192.168.1.105'Returned Non-zero exit status 1
Subprocess.check_output ()
Function prototypes: Check_output (*popenargs, **kwargs). Usage is the same as call (). The difference is that if the execution succeeds in returning the standard output content. If it fails, throw calledprocesserror. Exception.
Import= subprocess.check_output ('ping-n 2-w 3 192.168.1.104', shell= True)print output
2. OS module
Os.system ()
os.system (command). Calls the External System command, returns the command result code, but cannot get the command execution output result.
Import OS = Os.system ('ping-n 2-w 3 192.168.1.104')if retcode = = 0:< c9/>print"%s Success" % (IP,)else: Print " %s Fail " % (IP,)
Os.popen ()
Os.popen (command). Calls the External System command, returns the command to execute the output, but does not return the result?
Import OS = Os.popen ('ping-n 2-w 3 192.168.1.104')print output
3. Commands Module
The commands module mainly has the following 3 functions
or inch "ls-ld <file>" in a shell.
Examples of usage are as follows:
Import commands = commands.getstatusoutput ('ping-n 2-w 3 192.168.1.104')Print retcodeprint output
The results of the execution on Windows are as follows:
d:\temp>python test.py1'{' is not an internal or external command, nor is it a program or batch file that can be run.
The command execution result return code has failed to print for the 1,output variable value. Commands library can only be used on Linux?
Python calls External System commands