如何查看python 线程退出脚本自动退出原因

Python脚本示例[命令行参数,函数,判定,退出等]_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Python脚本示例[命令行参数,函数,判定,退出等]
来源:Linux社区&
作者:wklken
第一次根据需求写脚本
第一个版本,用于通用的数据转换
原数据为需要构造目标格式里面的几个字段,用某分隔符分开
目标数据为用指定分隔符分割的字段,源文件字段填充其间,其他字段为0
主要涉及命令行参数的处理和文件操作
import&os,getopt,sys&&
def&read_file(path):&&
&&f&=&open(path,"r")&&
&&lines&=&f.readlines()&&
&&f.close()&&
&&return&lines&&
def&one_line_proc(parts,total,to,outsp):&&
&&&&toindex&=&0&&
&&&&outline=""&&
&&&&for&i&in&range(1,total+1):&&
&&&&&&if&toindex!=len(to)&and&i==to[toindex]:&&
&&&&&&&&outline+=parts[toindex]&&
&&&&&&&&toindex+=1&&
&&&&&&else:&&
&&&&&&&&outline+="0"&&
&&&&&&if&i!=total:&&
&&&&&&&&outline+=outsp&&
&&&&return&outline&&
def&process(inpath,total,to,outpath,insp="\t",outsp="\t"):&&
&&lines&=&read_file(inpath)&&
&&f&=&open(outpath,"w")&&
&&result=[]&&
&&for&line&in&lines:&&
&&&&&parts&=&line.strip("\n").split(insp)&&
&&&&&if&len(parts)&==&len(to):&&
&&&&&&&outline&=&one_line_proc(parts,total,to,outsp)&&
&&&&&&&result.append(outline+"\n")&&
&&f.writelines(result)&&
&&f.close()&&
def&main():&&
&&&&try:&&
&&&&&&&&opts,args&=&getopt.getopt(sys.argv[1:],"F:P:t:a:i:o:")&&
&&&&&&&&if&len(opts)&&&3:&&
&&&&&&&&&&print("the&mount&of&params&must&great&equal&than&3")&&
&&&&&&&&&&sys.exit(1)&&
&&&&&&&&for&op,value&in&opts:&&
&&&&&&&&&&if&op&==&"-i":&&
&&&&&&&&&&&&inpath&=&value&&
&&&&&&&&&&elif&op&==&"-o":&&
&&&&&&&&&&&&outpath&=&value&&
&&&&&&&&&&elif&op&==&"-t":&&
&&&&&&&&&&&&total&=&int(value)&&
&&&&&&&&&&elif&op&==&"-a":&&
&&&&&&&&&&&&to&=&value.split(",")&&
&&&&&&&&&&elif&op&==&"-F":&&
&&&&&&&&&&&&insp&=&value.decode("string_escape")&&
&&&&&&&&&&elif&op&==&"-P":&&
&&&&&&&&&&&&outsp&=&value.decode("string_escape")&&
&&&&&&&&&&
&&&&&&&&&&
&&&&except&getopt.GetoptError:&&
&&&&&&&&print("params&are&not&defined&well!")&&
&&&&if&'outpath'&not&in&dir():&&
&&&&&&outpath&=&inpath+".dist"&&
&&&&if&'inpath'&not&in&dir():&&
&&&&&&print("-i&param&is&needed,input&file&path&must&define!")&&
&&&&&&sys.exit(1)&&
&&&&if&'total'&not&in&dir():&&&
&&&&&&print("-t&param&is&needed,the&fields&of&result&file&must&define!")&&
&&&&&&sys.exit(1)&&
&&&&if&'to'&not&in&dir():&&
&&&&&&print("-a&param&is&needed,must&assign&the&field&to&put&!")&&
&&&&&&sys.exit(1)&&
&&&&if&not&os.path.exists(inpath):&&
&&&&&&print("file&:&%s&is&not&exists"%inpath)&&
&&&&&&sys.exit(1)&&
&&&&tmp=[]&&
&&&&for&st&in&to:&&
&&&&&&tmp.append(int(st))&&
&&&&to=tmp&&
&&&&if&'insp'&in&dir()&and&'outsp'&in&dir():&&
&&&&&&process(inpath,total,to,outpath,insp,outsp)&&
&&&&elif&'insp'&in&dir():&&
&&&&&&process(inpath,total,to,outpath,insp)&&
&&&&elif&'outsp'&in&dir():&&
&&&&&&process(inpath,total,to,outpath,outsp=outsp)&&
&&&&else:&&
&&&&&&process(inpath,total,to,outpath)&&
相关资讯 & & &
& (01月26日)
& (12/11/:46)
& (10/06/:35)
& (01月03日)
& (11/26/:21)
& (10/04/:39)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款使用Python脚本查看Linux上的进程信息 - Plight - 51Testing软件测试网 51Testing软件测试网-中国软件测试人的精神家园
广交好友~~ 想要讨论的可以留下msn~~~
希望群友网友经常能提出问题,一起解决,共同提高
使用Python脚本查看Linux上的进程信息
& 16:48:37
/ 个人分类:
平台: Python 2.4.4, Fedora Core 6Python是一门易学, 开源的脚本语言, 下面介绍使用Python脚本查看上的进程信息.Python模块是Python语言中的一个重要概念,比如commands模块主要封装了命令相应的方法及数据. 我们可以通过import指令导入模块. 下面实例中用到了命令, 系统和字符串模块.Queryprocess.py的主要功能是查看Linux上指定进程的控制终端(tty), 所有者(uid), 进程号(pid), 父进程号(ppid)和进程开始运行时间(stime).创建python脚本文件queryprocess.pyislab$ vi queryprocess.py增加下面源代码到queryproces.py中#!/usr/bin/env python# Query process returns the following information: tty, uid, pid, ppid and stime.import commands, os, stringprogram = raw_input(”Enter the name of your process to query: “)try:#excute the ps command and assign results to a listutput = commands.getoutput(”ps -ef|grep ” + program)proginfo = string.split(output)#display resultsprint “\n\Terminal:\t\t”, proginfo[5], “\n\Owning User:\t\t\t”, proginfo[0], “\n\Process ID:\t\t”, proginfo[1], “\n\Parent Process ID:\t”, proginfo[2], “\n\System Time:\t\t”, proginfo[4]except:print “There is a problem with the process u are querying.”运行querprocess.pyislab$ chmod +x queryprocess.pyislab$ ./queryprocess.pyEnter the name of your process to query: syslogdTerminal: ?Owning User: rootProcess ID: 2387Parent Process ID: 1System Time: Aug09从上面结果我们看到syslogd进程的所有者是root用户, 进程号为2387, 父进程号是1, 进程开始运行时间是8月9日.同样我们也可以linux命令行运行下面命令获得上面信息:islab$ ps -ef|grep syslogdroot
00:00:01 syslogd -m 0对比两种方法的执行结果后我们可以看到用python能够更加友好的输出信息.2012年1月 其他开发语言大版内专家分月排行榜第二2011年5月 其他开发语言大版内专家分月排行榜第二2010年12月 其他开发语言大版内专家分月排行榜第二2009年2月 其他开发语言大版内专家分月排行榜第二2008年9月 其他开发语言大版内专家分月排行榜第二2008年8月 其他开发语言大版内专家分月排行榜第二2008年5月 其他开发语言大版内专家分月排行榜第二2007年11月 其他开发语言大版内专家分月排行榜第二
2011年4月 其他开发语言大版内专家分月排行榜第三2011年1月 其他开发语言大版内专家分月排行榜第三2009年6月 其他开发语言大版内专家分月排行榜第三2009年4月 其他开发语言大版内专家分月排行榜第三2009年1月 其他开发语言大版内专家分月排行榜第三2008年11月 其他开发语言大版内专家分月排行榜第三2008年7月 其他开发语言大版内专家分月排行榜第三2008年6月 其他开发语言大版内专家分月排行榜第三2006年9月 其他开发语言大版内专家分月排行榜第三
2012年1月 其他开发语言大版内专家分月排行榜第二2011年5月 其他开发语言大版内专家分月排行榜第二2010年12月 其他开发语言大版内专家分月排行榜第二2009年2月 其他开发语言大版内专家分月排行榜第二2008年9月 其他开发语言大版内专家分月排行榜第二2008年8月 其他开发语言大版内专家分月排行榜第二2008年5月 其他开发语言大版内专家分月排行榜第二2007年11月 其他开发语言大版内专家分月排行榜第二
2011年4月 其他开发语言大版内专家分月排行榜第三2011年1月 其他开发语言大版内专家分月排行榜第三2009年6月 其他开发语言大版内专家分月排行榜第三2009年4月 其他开发语言大版内专家分月排行榜第三2009年1月 其他开发语言大版内专家分月排行榜第三2008年11月 其他开发语言大版内专家分月排行榜第三2008年7月 其他开发语言大版内专家分月排行榜第三2008年6月 其他开发语言大版内专家分月排行榜第三2006年9月 其他开发语言大版内专家分月排行榜第三
2012年1月 其他开发语言大版内专家分月排行榜第二2011年5月 其他开发语言大版内专家分月排行榜第二2010年12月 其他开发语言大版内专家分月排行榜第二2009年2月 其他开发语言大版内专家分月排行榜第二2008年9月 其他开发语言大版内专家分月排行榜第二2008年8月 其他开发语言大版内专家分月排行榜第二2008年5月 其他开发语言大版内专家分月排行榜第二2007年11月 其他开发语言大版内专家分月排行榜第二
2011年4月 其他开发语言大版内专家分月排行榜第三2011年1月 其他开发语言大版内专家分月排行榜第三2009年6月 其他开发语言大版内专家分月排行榜第三2009年4月 其他开发语言大版内专家分月排行榜第三2009年1月 其他开发语言大版内专家分月排行榜第三2008年11月 其他开发语言大版内专家分月排行榜第三2008年7月 其他开发语言大版内专家分月排行榜第三2008年6月 其他开发语言大版内专家分月排行榜第三2006年9月 其他开发语言大版内专家分月排行榜第三
2012年1月 其他开发语言大版内专家分月排行榜第二2011年5月 其他开发语言大版内专家分月排行榜第二2010年12月 其他开发语言大版内专家分月排行榜第二2009年2月 其他开发语言大版内专家分月排行榜第二2008年9月 其他开发语言大版内专家分月排行榜第二2008年8月 其他开发语言大版内专家分月排行榜第二2008年5月 其他开发语言大版内专家分月排行榜第二2007年11月 其他开发语言大版内专家分月排行榜第二
2011年4月 其他开发语言大版内专家分月排行榜第三2011年1月 其他开发语言大版内专家分月排行榜第三2009年6月 其他开发语言大版内专家分月排行榜第三2009年4月 其他开发语言大版内专家分月排行榜第三2009年1月 其他开发语言大版内专家分月排行榜第三2008年11月 其他开发语言大版内专家分月排行榜第三2008年7月 其他开发语言大版内专家分月排行榜第三2008年6月 其他开发语言大版内专家分月排行榜第三2006年9月 其他开发语言大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。用户名:Leexide
文章数:30
访问量:14372
注册日期:
阅读量:1297
阅读量:3317
阅读量:452678
阅读量:1137136
51CTO推荐博文
#!/usr/bin/env python
# -*- coding=utf-8 -*-
#Using GPL v2.6
# Monitoring per-process disk I/O activity
import sys
import time
import signal
class DiskIO:
def __init__(self, pname=None, pid=None, reads=0, writes=<span style="color: #):
self.pname = pname
self.pid = pid
self.reads = 0
self.writes = 0
def main():
argc = len(sys.argv)
if argc != 1:
print &usage: please run this script like [./diskio.py]&
sys.exit(0)
if os.getuid() != 0:
print &Error: This script must be run as root&
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
os.system('echo 1 & /proc/sys/vm/block_dump')
print &TASK
while True:
os.system('dmesg -c & /tmp/diskio.log')
f = open('/tmp/diskio.log', 'r')
line = f.readline()
while line:
m = re.match(\
'^(\S+)\((\d+)\): (READ|WRITE) block (\d+) on (\S+)', line)
if m != None:
l.append(DiskIO(m.group(1), m.group(2)))
line = f.readline()
found = False
for item in l:
if item.pid == m.group(2):
found = True
if m.group(3) == &READ&:
item.reads = item.reads + 1
elif m.group(3) == &WRITE&:
item.writes = item.writes + 1
if not found:
l.append(DiskIO(m.group(1), m.group(2)))
line = f.readline()
time.sleep(1)
for item in l:
print &%-10s %10s %10d %10d& % \
(item.pname, item.pid, item.reads, item.writes)
def signal_handler(signal, frame):
os.system('echo 0 & /proc/sys/vm/block_dump')
sys.exit(0)
if __name__==&__main__&:
输出结果:
[root@oracle12c python]# ./diskio.py
<span style="color: #18
<span style="color: #
<span style="color: #
<span style="color: #70
<span style="color: #
<span style="color: #
<span style="color: #72
<span style="color: #
<span style="color: #
jbd2/sda6-<span style="color: #
<span style="color: #0
<span style="color: #
<span style="color: #
<span style="color: #74
<span style="color: #
<span style="color: #
<span style="color: #77
<span style="color: #
<span style="color: #
<span style="color: #79
<span style="color: #
<span style="color: #
jbd2/sda3-<span style="color: #
<span style="color: #40
<span style="color: #
<span style="color: #
jbd2/sda3-<span style="color: #
<span style="color: #40
<span style="color: #
<span style="color: #
<span style="color: #81
<span style="color: #
<span style="color: #
<span style="color: #83
<span style="color: #
<span style="color: #
jbd2/sda6-<span style="color: #
<span style="color: #0
<span style="color: #
<span style="color: #
了这篇文章
类别:┆阅读(0)┆评论(0)

我要回帖

更多关于 python 退出函数 的文章

 

随机推荐