2014-10-17 62 views
0

目标:写出一个没有给出新的文件,但没有错误或者

  1. 采取argparse参数,
  2. 测试,如果这样的说法是真实的
  3. 如果是真的,写一个文件在命令行 :

    这样的说法

例如后指定的名称

$蟒蛇printfile.py --out_arg fileOutput.txt

...会产生fileOutput.txt在同一目录printfile.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") 

然而, ,当我从def main()中调用print_output(未显示)传递感兴趣的元组(seqID,seq,comm)时,没有写入文件,也没有给出错误消息。它是不是将输入文件存储为dest的argparse参数?尝试写入时是否使用文件句柄?

+1

什么是'options.out_33'在这里? – 2014-10-17 18:20:56

+1

并且'print_output'实际*被调用*? – 2014-10-17 18:21:23

+0

是什么实际上称为? – 2014-10-17 18:23:57

回答

2

您从未在输出文件上调用close。 Python的写作在某种程度上是缓冲的,如果你不调用flushclose,那么你不能保证在文件中有所有的输出(或者对于短文件来说是一个文件)。

你应该总是使用with open() as ofile:语法以确保文件文件IO正确冲洗/关闭:

if options.out_arg: 
    with open(options.out_33, 'a') as outputfh: 
     outputfh.write(...) 
else: 
    ... 

当然,所有这一切都假定你实际上调用print_output是什么地方,你的代码没有显示。而options.out_33是一个相对路径,而不是绝对路径,否则该文件不会在您期望的位置出现。

相关问题