python中使用subprocess.Popen中的返回值总结:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import subprocess
import traceback


#author by zhangheng
#timestamp 2018/06/08
#gennlife hive script datagroup


# 打开文件
try:
    fo = open("zhangheng/python/core_field.txt", "r")  #字段验证
    fh = open("core_field.log", "w")  #字段验证日志输出
    lines  = fo.readlines()
    errorlog = open("error.log", "w")  #字段验证日志输出
    print len(lines)
    print '开始执行sql文档,将不会打印动态日志,如果执行失败,会将sql以及失败原因写入log日志文件,英文日志将存放于error.log中:'
    for cmd in lines:    
        print cmd
        try:
            p = subprocess.Popen(cmd,shell=True, close_fds=True, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
            while True:
                stdout,stderr = p.communicate()  #记录错误日志文件
                returncode = p.returncode
            #    print returncode
                if returncode == 0:
                    print '执行结果:执行成功!'
                 #   fh.write('执行结果:执行成功!\r\n')
                if returncode != 0:
                    errors = stderr.split('\n')
                    for erroline in errors :
                        if 'FAILED' in erroline:
                           errorlog.write(cmd)  #写入错误行查询语句
                           errorlog.write(erroline)  #写入错误行英文日志
                    fh.write(cmd)
                if returncode == 127:
                    print '执行结果:此行为空字符串!'
                    fh.write('执行结果:此行为空字符串!\r\n')  #中文日志
                if returncode == 17:
                    print '执行结果:找不到表异常'
                    fh.write('执行结果:找不到表\r\n')  #中文日志
                if returncode == 64:
                    print '执行结果:sql语句异常,缺失关键字'
                    fh.write('执行结果:sql语句错误!缺失关键字\r\n')  #中文日志
                if returncode == 41:
                    print '执行结果:sql语句异常,查询的字段不存在'
                    fh.write('执行结果:sql语句异常!查询的字段不存在\r\n')  #中文日志
                if stdout == '' and p.poll() != None:
                    break
        except Exception,re:
            print "调用hive客户端,执行hive语句出错"
            print str(re)
except Exception,re:
        print "读取文件错误!"
finally:
    fh.close()
    errorlog.close()
以上代码中returncode为执行返回结果的标志

我总结的有以下几种情况

returncode=0 表示执行成功

returncode=127 表示语句为空串

returncode=17 表示找不到表

returncode=64 表示缺失关键字

returncode=41 表示查询的字段不存在
--------------------- 
作者:zhangheng007a 
来源:CSDN 
原文:https://blog.csdn.net/weixin_38120374/article/details/80607369 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值