添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

如果有疑问,大家可以copy我的脚本自行进行验证。

首先,按照国际惯例,贴上我的代码#!/usr/bin/python# -*- coding: UTF-8 -*-import sysimport subprocessimport traceback# 打开文件try:    fo = open("/home/hadoop/zhangheng/python/core_field.txt", "r")    lines  = fo.readlines...
以前我一直用os.system()处理一些系统管理任务,因为我认为那是运行linux命令最简单的方式. 我们能从 Python 官方文档里读到应该用 subprocess 模块来运行系统命令. subprocess 模块允许我们创建子进程,连接他们的输入/输出/错误管道,还有获得 返回值 subprocess 模块打算来替代几个过时的模块和函数,比如: os.system, os.spawn*, os. popen *, popen 2.*命令。 让我们来看一下 subprocess 有哪些不同的函数. subprocess .call() 执行由参数提供的命令. 我们可以用数组作为参数运行命令,也可以用字符串
python 2.4版本开始,可以用 subprocess 这个模块来产生子进程,并连接到子进程的标准输入/输出/错误 去,还可以得到子进程的 返回值 subprocess 意在替代其他几个老的模块或者函数,比如:os.system os.spawn* os. popen * popen 2.* commands.* 一、 subprocess . Popen subprocess 模块定义了一个类: Popen class subprocess . Popen ( args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=
执行系统命令,可以获取执行系统命令的结果 p = subprocess . Popen ('ps aux',shell=True,stdout= subprocess .PIPE) out,err = p.communicate() for line in out.splitlines():
python subprocess . Popen 总结 subprocess 的目的就是启动一个新的进程并且与之通信。 subprocess 模块 只定义了一个类: Popen 。可以 使用 Popen 来创建进程,并与进程进行复杂的交互。它的构造函数如下: subprocess . Popen (args, bufsize=0, executable=None, stdin=None,
import subprocess proc = subprocess . Popen ([‘ python ’, ‘test.py’], stdout= subprocess .PIPE) while 1: print proc.poll() #while 1: print “hello” print “hello” 测试代码如上,poll函数返回码: 0 正常结束 1 sleep 2 子进程不存在 -15 kill None 在运行 poll的返回: A None value indicates that the p
ret_data = ret.read() # 2. subprocess 模块 ret = subprocess . Popen ('supervisorctl status',shell=True,stdout= subprocess .PIPE) out,err = ret.communicate() # 3.commands模块 ret_data = com
如果你在运行命令时包含了空格,则需要将命令作为一个列表传递给 ` subprocess . Popen ()` 函数。例如,如果你要运行命令 `ls -l /path/to/directory`,则可以 使用 以下代码: import subprocess command = ["ls", "-l", "/path/to/directory"] process = subprocess . Popen (command, stdout= subprocess .PIPE) output, error = process.communicate() print(output.decode()) 这将创建一个名为 `command` 的列表,其 第一个元素是命令名称,后续元素是命令的参数。你可以将该列表传递给 ` subprocess . Popen ()` 函数,并 使用 `stdout= subprocess .PIPE` 参数来捕获命令的输出。最后, 使用 `communicate()` 方法来等待命令执行完成并获取输出。