Python 3 Advanced | calling external third-party programs
Click here to learn the following while watching the video explanation
Python is often used to develop automated programs.
Automated programs often need to call other programs.
For example, we need to develop an automatic installation system program, in which one step {needs to download a file from the network.
If we develop the code to download files and realize the functions including breakpoint continuation, it will take a lot of time.
The download file has a ready-made very good tool wget. It can realize functions such as efficient downloading of large files, breakpoint continuation, etc.
At this time, we can call the wget program in the code instead of developing and downloading the code ourselves.
This is what we often do. In our Python program, we can} call other programs to help us realize functions.
Python calls external programs mainly through two methods, one is the system function of the os library, and the other is the subprocess library.
os.system function
It is very convenient to call other programs by using the {system} function of the os library.
Just take the contents of the command line as the parameters of the {system} function
For example, we need to use wget to download the nginx installation package, if it is executed on the command line
d:\tools\wget http://mirrors.sohu.com/nginx/nginx-1.13.9.zip
So calling in Python program can be like this.
import os cmd = r'd:\tools\wget http://mirrors.sohu.com/nginx/nginx-1.13.9.zip' os.system(cmd) print('Download complete')
wget on Windows can click here Download. This program does not need to be installed, but can be used directly on the command line
How about running the Python program above? Just download it?
The command line of the calling program is a string. So if the command needs any parameters, we can add it to the string.
For example, when we install the test environment, the name of the installation package will often change due to different} version numbers.
At runtime, let the user enter the version and then fill it in the command line string.
For example:
import os version = input('Please enter the installation package version:') cmd = fr'd:\tools\wget http://mirrors.sohu.com/nginx/nginx-{version}.zip' os.system(cmd) print('Download complete')
Careful students may find that when the above program runs, they have to wait until the download is completed, and finally print out the sentence "download is completed".
Originally, OS When the system function calls an external program, the code will not be executed until the execution of the called program is completed. Otherwise it will wait all the time.
os. The system function cannot get the contents of the called program output to the terminal window. If you need to process the output information of the called program, you can use the {subprocess} module
os.startfile function
If we want to achieve the effect similar to double clicking a file browser to open a file, we can use OS The startfile() function.
The argument to this function can be any non executable program file
os.startfile('d:\\statistical data.xlsx')
You can call the xlsx corresponding associated program (Excel) to open the file.
subprocess module
Click here to learn the following while watching the video explanation
The subprocess module provides more functions to call external programs.
First, we can get the output of external programs to the screen. This is very useful in automation. It can be used to judge whether the execution result of external programs is successful, or to obtain the data we want to analyze.
For example, we need to analyze whether the available space of Disk C is less than 10%. If it is insufficient, it will show that the space is urgent.
You can use the Popen class in the subprocess,
As follows (Note: this code should be run with administrator privileges)
from subprocess import PIPE, Popen # The Popen instance object is returned proc = Popen( 'fsutil volume diskfree c:', stdin = None, stdout = PIPE, stderr = PIPE, shell=True) # The communicate method returns the contents of the byte string output to standard output and standard error # Standard output device and standard error device are current terminal devices outinfo, errinfo = proc.communicate() # Note that the returned content is bytes, not str, and mine is Chinese windows, so gbk is used for decoding outinfo = outinfo.decode('gbk') errinfo = errinfo.decode('gbk') print (outinfo) print ('-------------') print (errinfo) outputList = outinfo.splitlines() # Residual quantity free = int(outputList[0].split(':')[1].replace(',','').split('(')[0].strip()) # Total space total = int(outputList[1].split(':')[1].replace(',','').split('(')[0].strip()) if (free/total < 0.1): print('!! The remaining space is urgent!!') else: print('Enough space left')
The communicate method returns two string objects. Corresponding to the byte string contents output by the called program to {standard output} and {standard error} respectively.
When the command-line program runs, it will automatically open the standard output device file and standard error device file. By default, both correspond to the current terminal device. We can simply understand it as a screen.
So what this method returns is the content displayed on the screen. But this returns a string of bytes, not a str string.
As we learned earlier, to decode bytes into str, you need to call the decode method. My is Chinese windows, so usually the output of external programs is gbk encoded byte string. So the parameter is specified as gbk.
Sometimes, after starting an external program, our Python program itself does not need to wait for the external program to end.
For example, we start the wget download command to download a file. Just let it download, and then our program will continue to do other tasks.
At this time, we can't use OS System, because it waits for the external program to end.
We can use Popen in subprocess, like this
from subprocess import Popen proc = Popen( args='wget http://xxxxserver/xxxx.zip', shell=True ) print ('Let it download and we'll do something else....')