Using Python to call external system commands can improve coding efficiency. After calling the external system command, you can obtain the return result code of command execution and the output result of command execution for further processing. This article mainly describes the common methods of calling external system commands in Python, including OS system(),os.popen(),subprocess. Popen et al.
This paper analyzes that python calls external system commands mainly from two aspects: 1. Whether the command execution result code can be returned, because most scenarios need to judge whether the call command is successful or failed. 2. Whether the command execution results can be obtained. In some scenarios, external commands are called to obtain the output results, and the output results can also be used to judge whether the command execution is successful or failed. The analysis results are as follows:
Next, the usage methods and examples of each function are described in detail.
1. subprocess module
The subprocess module is given priority because it can replace the old module, such as OS System (), os.popen(), etc. are recommended. The subprocess module can call external system commands to create new subprocesses. At the same time, it can connect to the nput/output/error pipeline of the subprocess and get the return value of the subprocess. The subprocess module mainly includes call () and check_call(),check_ The output() and Popen() functions are briefly described as follows:
Main API ======== call(...): Runs a command, waits for it to complete, then returns the return code. check_call(...): Same as call() but raises CalledProcessError() if return code is not 0 check_output(...): Same as check_call() but returns the contents of stdout instead of a return code Popen(...): A class for flexibly executing a command in a new process Constants --------- PIPE: Special value that indicates a pipe should be created STDOUT: Special value that indicates that stderr should go to stdout
Let's start with the use of the subprocess function.
(1) subprocess.Popen class
subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
args: The external system command to call. bufsize: The default value is 0, Indicates no caching,. A value of 1 indicates a row cache,. Other positive numbers indicate the size used by the cache,，negative-1 Indicates that the system default cache size is used. stdin,stdout,stdout Represents standard input, standard output and standard error respectively. Its value can be PIPE,File descriptors and None Wait. The default value is None，Represents inheritance from the parent process. shell Linux: The parameter value is False When, Linux By calling os.execvp Execute the corresponding procedure. by Trule When, Linux Direct call system on shell To execute the program. Windows: shell Parameter indicates whether to use bat As an execution environment. Only execution windows of dir,copy Wait for the command to be set to True. Other procedures are no different. executable Used to specify an executable program. Normally we pass args Parameter to set the program to run. If the parameter shell Set as True，executable Will specify the program to use shell. stay windows Platform, default shell from COMSPEC Environment variable. preexec_fn Only in Unix Valid under platform, used to specify an executable object( callable object)，It will be called before the child process runs cwd Set current directory of child process env env Is a dictionary type used to specify the environment variables of child processes. The default value is None，The environment variable representing the child process is inherited from the parent process. Universal_newlines The newline character of text is different under different operating systems. For example: windows Next use'/r/n'Means change, and Linux Next use '/n'. If this parameter is set to True，Python Uniformly treat these line breaks as'/n'To handle it.
The properties and methods corresponding to the Popen object are as follows:
attribute: stdin, stdout, stderr, pid, returncode method: communicate(self, input=None) -> returns a tuple (stdout, stderr). wait(self) -> Wait for child process to terminate. Returns returncode attribute.
1. Print the D:\temp directory and create the test directory. Directly call the process, regardless of obtaining the output content and result code of the call command
import subprocess p = subprocess.Popen(args='mkdir test', shell=True, cwd='d:/temp') p.wait()
2. Call the ping command to execute and obtain the command execution output
import subprocess p = 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()
Note: p.stdout, p.stdin and p.stderr are file objects. You can use file object functions, such as read().
Function prototype: call(*popenargs, **kwargs). call() calls the external system command to execute and returns the program execution result code.
import subprocess retcode = subprocess.call('ping -n 2 -w 3 192.168.1.104', shell=True) print retcode
Use the same method as call(). If the calling command is executed successfully, the result code 0 is returned. If the execution fails, a CalledProcessError is thrown Abnormal. Examples are as follows:
>>> p = subprocess.check_call('ping -n 2 -w 3 192.168.1.105', shell=True) Is Ping 192.168.1.105 Data with 32 bytes: The request timed out. The request timed out. 192.168.1.105 of Ping statistical information : data packet: has been sent = 2，Received = 0，lose = 2 (100% lose)， Traceback (most recent call last): File "<stdin>", line 1, in <module> File "c:\Python27\lib\subprocess.py", line 186, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command 'ping -n 2 -w 3 192.168.1.105' returned non-zero exit status 1
Function prototype: check_output(*popenargs, **kwargs). The usage is the same as call(). The difference is that if the execution is successful, the standard output is returned. If it fails, throw CalledProcessError Abnormal.
import subprocess output = subprocess.check_output('ping -n 2 -w 3 192.168.1.104', shell=True) print output
2. os module
os.system(command) . Call the external system command and return the command result code, but the command execution output result cannot be obtained, and the output result is directly printed to the screen terminal.
import os retcode = os.system('ping -n 2 -w 3 192.168.1.104') if retcode == 0: print "%s Success" % (ip,) else: print "%s Fail" % (ip,)
os.popen(command) . Call the external system command and return the command execution output result, but do not return the result
import os output = os.popen('ping -n 2 -w 3 192.168.1.104') print output
3. commands module
The commands module is used to invoke Linux shell commands. The test failed on windows. There are three main functions
getoutput(cmd): Return output (stdout or stderr) of executing cmd in a shell. getstatus(file): Return output of "ls -ld <file>" in a string. getstatusoutput(cmd): Return (status, output) of executing cmd in a shell.
Examples are as follows:
import commands retcode, output = commands.getstatusoutput('ping -n 2 -w 3 192.168.1.104') print retcode print output
When writing a program, you can select different Python call methods to execute external system commands according to the use scenario. For complex commands, consider using subprocess Popen() is completed. If it is only a simple command execution, you can use OS System() is completed, such as calling the windows suspend program command OS system('pause').