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

时间:2023-03-09 04:06:36
>Python下使用subprocess中文乱码的解决方案
  1. # -*- coding: CP936 -*-
  2. import subprocess
  3. cmd="cmd.exe"
  4. begin=101
  5. end=110
  6. while begin<end:
  7. #reload(sys)
  8. #sys.setdefaultencoding('CP936')
  9. p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)
  10. p.stdin.write("echo 你好."+str(begin)+"\n")
  11. p.stdin.close()
  12. p.wait()
  13. print"excution result:\n"
  14. pout=''.join(p.stdout.readlines())
  15. output=pout.decode('cp936').encode('utf-8')
  16. print"%s\n"% output
  17. begin=begin+1