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类型

 

posted on 2019-05-26 22:20  lilyxiaoyy  阅读(1065)  评论(0编辑  收藏  举报

返回
顶部