当前位置:   article > 正文

SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xd0 in position 2: invalid continuatio...

syntaxerror: (unicode error) 'utf-8' codec can't decode byte 0xd2 in positio

[root@hostuser src]# python3 subprocess_popen.py
File "subprocess_popen.py", line 23
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xd0 in position 2: invalid continuation byte

 

  1. #!/usr/bin/python3
  2. # coding=gbk
  3. import os
  4. import sys
  5. curPath = os.path.abspath(os.path.dirname(__file__))
  6. rootPath = os.path.split(curPath)[0]
  7. sys.path.append(rootPath)
  8. import subprocess
  9. from src import logutils
  10. def run():
  11. log=logutils.logger("app",rootstdout=True,handlerList=['I','E'])
  12. str_shell='df -m && netstat -ntlp'
  13. sub=subprocess.Popen(args=str_shell,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,
  14. stderr=subprocess.PIPE,universal_newlines=True)
  15. out,err=sub.communicate()
  16. #res=sub.stdout.readlines()
  17. #log.info(res)
  18. if sub.returncode == 0:
  19. #log.info("returncode is 0,执行输出正常")
  20. if out:
  21. log.info("执行输出正常")
  22. log.info(out)
  23. if err:
  24. log.error("出现异常")
  25. log.error(err,exc_info=True)
  26. else:
  27. if sub.returncode == 1:
  28. log.error("执行shell对象结果有空")
  29. else:
  30. raise subprocess.CalledProcessError(sub.returncode, str_shell)
  31. if __name__ == '__main__':
  32. run()

  

 
 

起初# -*- coding:utf-8 -*- 再运行还是报错,加上#coding=gbk问题完美解决

 

 

关于subprocess.returncode:

我总结的有以下几种情况

returncode=0 表示执行成功

returncode=1,表示执行结果为空

returncode=127 表示语句为空串

returncode=17 表示找不到表

returncode=64 表示缺失关键字

returncode=41 表示查询的字段不存在


转载于:https://www.cnblogs.com/SunshineKimi/p/10889027.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/539680
推荐阅读
相关标签
  

闽ICP备14008679号