Python module - subprocess module

Introduction

There are several functions that create subprocesses. These functions create subprocesses in different ways, so we can choose one of them to use according to our needs.

usage

import subprocess
参数([命令][解释器])

subprocess.Popen()

The following functions are based on Popen() wrappers. The purpose of these wrappers is to make it easy for us to use subprocesses. When we want to personalize our needs more, we turn to the Popen class, which generates objects that represent child processes.

import subprocess
retcode = subprocess.Popen('dir',shell=True)
print(retcode)
<subprocess.Popen object at 0x0000021A4F0ADA90>
 驱动器 F 中的卷是 office
 卷的序列号是 25A0-6BA3

 F:\python\test 的目录

2018/05/03  10:48    <DIR>          .
2018/05/03  10:48    <DIR>          ..

subprocess.call()

Parent process waits for child process to finish

Return exit information (returncode, equivalent to Linux exit code)

import subprocess
retcode = subprocess.call('dir',shell=True)
print(retcode)
 驱动器 F 中的卷是 office
 卷的序列号是 25A0-6BA3

 F:\python\test 的目录

2018/05/03  10:43    <DIR>          .
2018/05/03  10:43    <DIR>          ..
0

subprocess.check_call()

Parent process waits for child process to finish

Check the exit information. If the returncode is not 0, the error subprocess.CalledProcessError will be raised. The object contains the returncode attribute, which can be checked by try...except...

import subprocess
retcode = subprocess.check_call('dir',shell=True)
print(retcode)
 驱动器 F 中的卷是 office
 卷的序列号是 25A0-6BA3

 F:\python\test 的目录

2018/05/03  10:43    <DIR>          .
2018/05/03  10:43    <DIR>          ..
0

subprocess.check_output()

The parent process waits for the child process to complete and returns the output of the child process to standard output

Check the exit information. If the returncode is not 0, an error subprocess.CalledProcessError will be raised. The object contains the returncode attribute and the output attribute. The output attribute is the output result of the standard output, which can be checked with try...except....

retcode = subprocess.check_output('dir',shell=True)
print(retcode)
b' \xc7\xfd\xb6\xaf\xc6\xf7 F \xd6\xd0\xb5\xc4\xbe\xed\xca\xc7 office\r\n \xbe\xed\xb5\xc4\xd0\xf2\xc1\xd0\xba\xc5\xca\xc7 25A0-6BA3\r\n\r\n F:\\python\\test \xb5\xc4\xc4\xbf\xc2\xbc\r\n\r\n2018/05/03  10:41    <DIR>          .\r\n2018/05/03  10:41    <DIR>          ..\r\n2018/05/03  10:41               165 sub.py\r\n2018/05/02  14:23               694 TCP\xbf\xcd\xbb\xa7\xb6\xcb.py\r\n2018/05/03  08:38             1,086 TCP\xb7\xfe\xce\xf1\xb6\xcb.py\r\n2018/05/03  09:18               687 UDP\xbf\xcd\xbb\xa7\xb6\xcb.py\r\n2018/05/03  09:18               721 UDP\xb7\xfe\xce\xf1\xb6\xcb.py\r\n2018/05/02  09:14                 0 __init__.py\r\n               6 \xb8\xf6\xce\xc4\xbc\xfe          3,353 \xd7\xd6\xbd\xda\r\n               2 \xb8\xf6\xc4\xbf\xc2\xbc 245,565,284,352 \xbf\xc9\xd3\xc3\xd7\xd6\xbd\xda\r\n'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297145&siteId=291194637