我不知道为什么下面的代码不能工作-我得到了错误
NameError: name 'group1' is not defined.
在我尝试使用getopt之前,代码运行得很好。。我试图解析命令行输入,以便
^{pr2}$file1和file2作为“group1”成为第一个循环的输入。在
import sys
import csv
import vcf
import getopt
#set up the args
try:
opts, args = getopt.getopt(sys.argv[1:], 'q:r:h', ['query', 'reference', 'help'])
except getopt.GetoptError as err:
print str(err)
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
print "Usage python -q [query files] -r [reference files]"
print "-h this help message"
elif opt in ('-q', '--query'):
group1 = arg
elif opt in ('-r', '--reference'):
group2 = arg
else:
print"check your args"
#extract core snps from query file, saving these to the set universal_snps
snps = []
outfile = sys.argv[1]
for variants in group1:
vcf_reader = vcf.Reader(open(variants))
问题是
group1 = arg
从来没有运行过,所以当它稍后到达for variants in group1:
时,变量没有被定义。在这是因为调用函数的方式与定义选项的方式不符。当你有台词时:
有一个要求是,带有标志的参数(即
^{pr2}$-q file1
和-r file3
必须在任何其他参数之前指定。因此,如果要将函数调用为:你会有预期的行为。这是因为没有关联标志的所有参数都出现在调用的末尾(并且可以通过
args
参数检索)相关问题
PyPI热门下载资源包