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


[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

 

#!/usr/bin/python3
# coding=gbk
import os
import sys
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
import subprocess
from src import logutils

def run():
    log=logutils.logger("app",rootstdout=True,handlerList=['I','E'])
    str_shell='df -m && netstat -ntlp'
    sub=subprocess.Popen(args=str_shell,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,universal_newlines=True)
    out,err=sub.communicate()
    #res=sub.stdout.readlines()
    #log.info(res)

    if sub.returncode == 0:
        #log.info("returncode is 0,執行輸出正常")
        if out:
          log.info("執行輸出正常")
          log.info(out)
        if err:
          log.error("出現異常")
          log.error(err,exc_info=True)
    else:
      if sub.returncode == 1:
          log.error("執行shell對象結果有空")
      else:
          raise subprocess.CalledProcessError(sub.returncode, str_shell)

      

if __name__ == '__main__':
    run()

  

 
        

起初# -*- coding:utf-8 -*- 再運行還是報錯,加上#coding=gbk問題完美解決

 

 

關於subprocess.returncode:

我總結的有以下幾種情況

returncode=0 表示執行成功

returncode=1,表示執行結果為空

returncode=127 表示語句為空串

returncode=17 表示找不到表

returncode=64 表示缺失關鍵字

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



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM