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