python调用linux shell脚本,并返回结果一例
2012-03-04 14:54
519 查看
#!/usr/bin/python#-*- coding:cp936 -*-import re;import os;import sys;if(__name__=="__main__"): p=re.compile('(^\s+|\s+$)'); pdigit=re.compile('^\d+'); delimiter='\t'; strdict='dict.dat'; strcontigency='contigency.dat'; str_input=str(sys.argv[1]); str_weight=str(sys.argv[2]); str_output=str(sys.argv[3]); fid_input=file(str_input,'r'); fid_output=file(str_output,'w'); keywords=[]; keywordstmp=fid_input.readlines(); for m in keywordstmp: mykey=p.sub('',m); keywords.append(mykey); for keyword in keywords: results=[]; #results.append(keyword); command=''; command='grep -n ^%s$ %s'%(keyword,strdict); tmpcol=os.popen(command).readlines(); temp=p.sub('',tmpcol[0]); temps=temp.split(':'); linenum=int(temps[0])+1; command="sed -n '%dp' %s"%(linenum,strdict); tmpcol=os.popen(command).readlines(); temp=p.sub('',tmpcol[0]) ;#indicate howmany docs key indexed; totalindexed=temp; #results.append(temp); command='grep -n ^%s$ %s'%(keyword,str_weight); tmpcol=os.popen(command).readlines(); temp=p.sub('',tmpcol[0]); temps=temp.split(':'); linenum=int(temps[0])+2; command="sed -n '%dp' %s"%(linenum,str_weight); tmpcol=os.popen(command).readlines(); temp=p.sub('',tmpcol[0]) ; temps=temp.split(' '); myclass=temps[0] #results.append(myclass); command="grep -n '^%s %s' %s"%(keyword,myclass,strcontigency); tmpcol=os.popen(command).readlines(); temp=p.sub('',tmpcol[0]); temps=temp.split(':'); temp1=p.sub('', temps[1]); results=temp1.split(' '); results.append(totalindexed); #numin=m[1]; #numnotin=m[2]; #print numin; #print numnotin; #print results #s=raw_input('please enter'); #results.append(numin); #results.append(numnotin); myline=delimiter.join(results); fid_output.write(myline); fid_output.write('\n'); print myline; fid_input.close(); fid_output.close(); print '%s has finished, congratulations!'%str(sys.argv[0]);
相关文章推荐
- python 调用mysql存储过程返回结果集
- python2.7调用mysql存储过程并且返回结果
- 让FLASH接收网页传过来的参数和动态调用外部脚本程序返回结果
- Java代码中调用shell脚本和python脚本并获得输出结果(分为小数据量和大数据量)
- python 调用mysql存储过程返回结果集
- C#如何调用Python执行脚本,并将执行结果显示值显示至C#界面
- 使用python写自动执行脚本并将结果返回到html中
- C#如何调用Python执行脚本,并将执行结果显示值显示至C#界面
- 自动旁注并多进程调用wwwscan扫描旁注结果的python脚本。
- python调用mysql中的自定义function并且返回结果
- java调用linux中的shell脚本并返回执行结果
- window下java调用python脚本并读取Python运行结果
- Objective_C 调用shell脚本并返回结果
- python脚本利用ctypes调用c模块返回字符串
- python实现调用其他python脚本的方法
- Android调用系统添加和编辑联系人的返回结果
- 使用VB调用Oracle程序包内的存储过程返回结果集
- 用脚本C调用Python脚本文件里的函数
- 【转】C#中调用python脚本
- WebView Android 调用js且须要获取返回结果