subprocess模块

subprocess模块:操作shell命令

import subprocess
order = subprocess.Popen('终端命令', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# order.stdout 流对象,order.stdout.read()来获取操作的信息字符串
suc_res = order.stdout.read().decode('系统默认编码')
err_res = order.stderr.read().decode('系统默认编码')

# stdout:存放指令执行成功的信息管道 | stderr 存放指令执行失败的信息管道
order = subprocess.run('终端命令', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# order.stdout 是字符串信息,就是Popen下order.stdout.read()
suc_res = order.stdout.decode('系统默认编码')
err_res = order.stderr.decode('系统默认编码')

原文地址:https://www.cnblogs.com/zhangdajin/p/11144624.html