【发布时间】:2014-12-13 09:31:09
【问题描述】:
目标:
- 在 argparse 中取一个参数,
- 测试该参数是否为真
- 如果为真,则使用该参数后指定的名称写入文件
例如: 在命令行中:
$ python printfile.py --out_arg fileOutput.txt
... 将在与 printfile.py 相同的目录中生成一个 fileOutput.txt
代码:
def parse_arguments():
options = parse_arguments()
#output arguments
parser.add_argument("--out_arg", action='store', default=False, dest='out_arg',
help="""Output file """)
def print_output(seqID, seq, comm):
# "a" append is used since this is the output of a for loop generator
if options.out_arg
outputfh = open(options.out_33,"a")
outputfh.write("@{}\n{}\n{}\n+".format(seqID, seq, comm))
else:
sys.stderr.write("ERR: sys.stdin is without sequence data")
然而,当我从 def main() 调用 print_output - 未显示 - 传入我感兴趣的元组(seqID、seq、comm)时,没有写入文件,也没有给出错误消息。是argparse参数没有将输入的文件存储为dest吗?尝试写入时是否使用文件句柄?
【问题讨论】:
-
这里的
options.out_33是什么? -
而
print_output实际上是调用吗? -
实际上叫什么?
-
'def parse_arguments()' 的第一行是'options = parse_arguments()'。你要无限递归吗??
-
@MartijnPieters options 指的是我的论点,而 out_33 是我对该特定论点的名称。在这种情况下,
--out_33 < filename >将在命令行中调用,意思是“以 --out_33 格式发送此文件,写入新文件”是 options.out_33 没有充分引用命令中传递的文件名那个参数之后的行?
标签: python python-2.7 command-line-arguments argparse sys