目标是:
例如: 在命令行中:
$ python printfile.py --out_arg fileOutput.txt
。。。会产生文件输出.txt在与相同的目录中打印文件.py你知道吗
代码:
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")
然而,当我调用print_output from def main()-未显示-传入感兴趣的元组(seqID,seq,comm)时,不会写入任何文件,也不会给出任何错误消息。是argparse参数没有将输入的文件存储为dest吗?在尝试写入时是否使用了文件句柄?你知道吗
您从未对输出文件调用
close
。Python的编写在某种程度上得到了缓冲,根据doc,如果不调用flush
或close
,就不能保证所有的输出都在文件中(或一个文件)。你知道吗您应该始终使用
with open() as ofile:
语法执行文件IO,以确保文件正确刷新/关闭:当然,所有这些都假设您实际上在某个地方调用
print_output
,而您的代码没有显示。而且options.out_33
是一个相对路径,而不是一个绝对路径,否则文件将不会在您期望的地方结束。你知道吗相关问题 更多 >
编程相关推荐