Python下使用subprocess中文乱码的解决方案

2020-12-08 11:15:17 浏览数 (1)

代码语言:javascript复制
# -*- coding: CP936 -*-
import subprocess

cmd="cmd.exe"
begin=101
end=110
while begin<end:
#reload(sys)
#sys.setdefaultencoding('CP936')
 p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)
 p.stdin.write("echo 你好." str(begin) "n")
 p.stdin.close()
 p.wait()
print"excution result:n"
 pout=''.join(p.stdout.readlines())
 output=pout.decode('cp936').encode('utf-8')
print"%sn"% output
 begin=begin 1

来自为知笔记(Wiz)

0 人点赞