我目前正在用python编写一个程序而且我被卡住了。所以我的任务是: 我有一个程序读取文件并打印一些行到stdout像这样:
#imports
import sys
#number of args
numArgs = len(sys.argv)
#ERROR if not enough args were committed
if numArgs <= 1:
sys.exit("Not enough arguments!")
#naming input file from args
Input = sys.argv[1]
#opening files
try:
fastQ = open(Input , 'r')
except IOError, e:
sys.exit(e)
#parsing through file
while 1:
#saving the lines
firstL = fastQ.readline()
secondL = fastQ.readline()
#you could maybe skip these lines to save ram
fastQ.readline()
fastQ.readline()
#make sure that there are no blank lines in the file
if firstL == "" or secondL == "":
break
#edit the Header to begin with '>'
firstL = '>' + firstL.replace('@' , '')
sys.stdout.write(firstL)
sys.stdout.write(secondL)
#close both files
fastQ.close()
现在我想重写这个程序,以便我可以运行命令行,如:zcat&#34; textfile&#34; | python&#34; myprogram&#34; &GT; &#34; otherfile&#34 ;.所以我环顾四周,找到了子流程,但似乎无法弄清楚如何去做。谢谢你的帮助
编辑:
现在,如果您正在尝试编写Python脚本来编排zcat和myprogram的执行,那么您可能需要子进程。 - rchang
打算使用&#34;文本文件&#34;和群集上的程序,所以我不需要从群集中复制任何文件。我只想登录群集并使用命令:zcat&#34; textfile&#34; | python&#34; myprogram&#34; &GT; &#34; otherfile&#34;,以便zcat和程序做他们的事情,我最终得到&#34; otherfile&#34;在群集上。希望你明白我想做什么。
编辑#2:
我的解决方案
#imports
import sys
import fileinput
# Counter, maybe there is a better way
count = 0
# Iterieration over Input
for line in fileinput.input():
# Selection of Header
if count == 0 :
#Format the Header
newL = '>' + line.replace('@' , '')
# Print the Header without newline
sys.stdout.write(newL)
# Selection of Sequence
elif count == 1 :
# Print the Sequence
sys.stdout.write(line)
# Up's the Counter
count += 1
count = count % 4
THX
答案 0 :(得分:0)
您可以使用fastQ = sys.stdin
从stdin而不是文件读取输入,或者(更一般地)fastQ = fileinput.input()
从stdin和/或命令行中指定的文件中读取。
还有fileinput.hook_compressed
,因此您不需要zcat
并直接读取压缩文件:
$ myprogram textfile >otherfile