Four Methods of Executing System Commands in python and Their Comparison

  • 2021-10-24 23:12:39
  • OfStack

There are four common ways to execute system commands in Python

Note: The following example code runs through Python 3.5.

1. os. system method

os.system(cmd)

When the system command is run in the sub-terminal, the return information after the command is executed and the state of the execution return can be obtained


>>> import os
>>> os.system('date')
2018 Year  4 Month  8 Day   Sunday  19 Hour 29 Points 13 Seconds  CST
0 # Running status number, 0 Express correctly 

After execution, two lines of results are returned, the first line is the result and the second line is the execution status information

2. os. popen method

os.popen(cmd)

Not only executes the command but also returns the information object after execution (often used to obtain the return information after execution of the command), which returns the result through a pipeline file


>>> import os
>>> nowtime = os.popen('date')
>>> print(nowtime.read())
2018 Year  4 Month  8 Day   Sunday  19 Hour 30 Points 35 Seconds  CST

3. commands module

getoutput: Get the information returned after executing the command

getstatus: Gets the status value of the execution command (returns a value of 0 if the execution command succeeds, otherwise returns a non-0)

getstatusoutput: Get the status value of the execution command and return information


>>> import commonds
>>> status, output = commands.getstatusoutput('date')
>>> print(status)  # 0
>>> print(output)  # 2018 Year  4 Month  8 Day   Sunday  19 Hour 31 Points 45 Seconds  CST

Note 1: The return value (status) returned by using this method in the system of class unix is different from the return value after the script or command is executed, which is due to the call of os. wait (), and the specific reason depends on the implementation of the system wait (). To get the correct return value (status), just shift the return value to the right by 8 bits.

Note 2: subprocess is recommended when Chinese text is included in the parameters of the execution command or in the return.

4. subprocess module

By controlling and monitoring the thread, the returned result is assigned to 1 variable, which is convenient for the program to deal with. There are abundant parameters to configure, and there are many options for us to customize, with high flexibility. When I used os. system, I encountered the problem that file descriptors were inherited by child processes, which was solved by the parameter close_fds = False. Official document: http://python.usyiyi.cn/python_278/library/subprocess. html


>>> import subprocess
>>> nowtime = subprocess.Popen('date', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
>>> print(nowtime.stdout.read())
2018 Year  4 Month  8 Day   Sunday  19 Hour 32 Points 41 Seconds  CST

This article mainly explains the four methods of python executing system commands and compares them. This is the end. Please see the related links below for more methods of python executing system commands


Related articles: