在 python 中找出管道
Figuring out pipes in python
我目前正在 python 中编写程序,但我被卡住了。所以我的问题是:
我有一个程序可以读取文件并将一些行打印到标准输出,如下所示:
#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 "textfile" | python "myprogram" > "otherfile"。所以我环顾四周,发现了子流程,但似乎无法弄清楚该怎么做。感谢您的帮助
编辑:
Now, if what you are doing is trying to write a Python script to orchestrate the execution of both zcat and myprogram, THEN you may need subprocess. – rchang
打算将 "textfile" 和程序放在集群上,因此我不需要从集群复制任何文件。我只想登录集群并使用 command:zcat "textfile" | python "myprogram" > "otherfile",以便 zcat 和程序执行它们的操作,我最终在集群上得到 "otherfile"。希望你明白我想做什么。
编辑#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
谢谢
您可以使用 fastQ = sys.stdin
从标准输入而不是文件读取输入,或者(更一般地)fastQ = fileinput.input()
从命令行指定的标准输入 and/or 文件读取。
还有fileinput.hook_compressed
这样就不需要zcat
直接读取压缩文件了:
$ myprogram textfile >otherfile
我目前正在 python 中编写程序,但我被卡住了。所以我的问题是: 我有一个程序可以读取文件并将一些行打印到标准输出,如下所示:
#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 "textfile" | python "myprogram" > "otherfile"。所以我环顾四周,发现了子流程,但似乎无法弄清楚该怎么做。感谢您的帮助
编辑:
Now, if what you are doing is trying to write a Python script to orchestrate the execution of both zcat and myprogram, THEN you may need subprocess. – rchang
打算将 "textfile" 和程序放在集群上,因此我不需要从集群复制任何文件。我只想登录集群并使用 command:zcat "textfile" | python "myprogram" > "otherfile",以便 zcat 和程序执行它们的操作,我最终在集群上得到 "otherfile"。希望你明白我想做什么。
编辑#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
谢谢
您可以使用 fastQ = sys.stdin
从标准输入而不是文件读取输入,或者(更一般地)fastQ = fileinput.input()
从命令行指定的标准输入 and/or 文件读取。
还有fileinput.hook_compressed
这样就不需要zcat
直接读取压缩文件了:
$ myprogram textfile >otherfile