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

[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 表示查询的字段不存在


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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值