python3 subprocess模块

Posted lilyxiaoyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3 subprocess模块相关的知识,希望对你有一定的参考价值。

import subprocess

cmd = input(">>>: ")
res = subprocess.Popen(
    cmd,  # 字符串指令,如dir 或 ipconfig等等
    shell=True,  # 使用shell,就相当于使用cmd窗口
    stderr=subprocess.PIPE,  # 标准错误输出,PIPE为管道
    stdout=subprocess.PIPE,  # 标准输出
)

print(res.stdout.read().decode("gbk"))  # window读出的就是GBK编码的
print(res.stderr.read().decode("gbk"))  # stdout.read()读出来的数据是bytes类型

 

以上是关于python3 subprocess模块的主要内容,如果未能解决你的问题,请参考以下文章