First, the role of subprocess
Subprocess is a module for executing system commands in a program, and the Popen method used in the main process to open a child process to execute a system command, unlike Os.system is that it can return execution results and os.system directly output execution results to the terminal.
Second, the use of subprocess
Subprocess The first parameter of the Popen method is a string-formatted system command, the remaining common parameters are whether the shell enables the command interpreter, stdout specifies the standard output location, STDERR specifies the standard error output location, It is important to note that the returned result is that the bytes type of data needs to be transcoded for normal viewing, and the transcoding code needs to be determined by the system character encoding.
Import subprocessres=subprocess. Popen ('dir'# executes the dir command, enabling the command interpreter, standard output to subprocess. PIPE, standard error output to subprocess. PIPEPrint(Res.stdout.read (). Decode ('gbk'# view standard output content Print(Res.stderr.read (). Decode ('gbk'# View standard error content
Subprocess of the Python module