从命令行,我正在尝试编写一个python脚本来读取由不同的相应标志引用的多个文件,以便我可以搜索每个文件的每一行以查找特定的文本字符串。到目前为止,我已经使用fileinput包为没有标志的单个文件完成了这个,并且它工作得很好。我曾尝试使用argparse,并且能够通过引用标志来读取单个文件,但我无法弄清楚如何使用两个单独的相应标志读取两个单独的文件。我非常感谢任何帮助。
这是我的fileinput方法的代码:
#!/usr/bin/python
import fileinput
anc=[]
for line in fileinput.input():
line=line.strip('\n')
if "i>" in line:
anc.append(line)
print anc
..这是我的argparse方法的代码:
#!/usr/bin/python
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("--file", "-a", type=str, required=True,
help="index.html file showing breseq polymorphism mutations between
ancestral sequences")
parser.add_argument("--file", "-e", type=str, required=True,
help="index.html file showing breseq polymorphism mutations between
ancestral and evolved sequences")
答案 0 :(得分:0)
如果要使用argparse
,可以使用nargs
参数捕获参数的多个值,例如:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-F", "--files", nargs="+", type=str, required=True,
help="whitespace delimited list of files...")
args = parser.parse_args()
for path in args.files:
# do whatever you want with individual file paths stored in `path`
pass
或者,如果您想单独使用多个参数(例如-f file_path1 -f file_path2 ...
),您可以在action="append"
参数中使用argparse
:
parser.add_argument("-f", "--file", action="append", type=str, required=True,
help="input file")
(记得在循环中更改为args.file
。)
一旦你迭代了你的论点,你根本不需要fileinput
模块,只需做一个简单的事情:
anc=[]
with open(path, "r") as f:
for line in f:
line = line.rstrip("\n")
if "i>" in line:
anc.append(line)
print(anc)
或者把它们放在一起:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-F", "--files", nargs="+", type=str, required=True,
help="whitespace delimited list of files...")
args = parser.parse_args()
anc=[]
for path in args.files:
with open(path, "r") as f:
for line in f:
line = line.rstrip("\n")
if "i>" in line:
anc.append(line)
print(anc)
更新 - 如果你只想要那两个参数,那么我就误解了你。这样更容易:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--ancestral", type=str, required=True,
help="file with ancestral data...")
parser.add_argument("-e", "--evolved", type=str, required=True,
help="file with evolved data...")
def process_file(path): # a simple function to process our files individually
result = []
with open(path, "r") as f:
for line in f:
line = line.rstrip("\n")
if "i>" in line:
result.append(line)
return result
args = parser.parse_args() # parse the args
anc = process_file(args.ancestral) # process the ancestral file
print(anc)
evo = process_file(args.evolved) # process the evolved file
print(evo)
答案 1 :(得分:0)
以下是我如何完成它:
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("-a", action="store", dest="anc_file")
parser.add_argument("-e", action="store", dest="evo_file")
args = parser.parse_args()
a=open(args.anc_file,"r")
e=open(args.evo_file,"r")
anc=[]
evo=[]
for line in a:
line=line.strip("\n")
if "i>" in line:
anc.append(line)
for line in e:
line=line.strip("\n")
if "i>" in line:
evo.append(line)
print anc, evo