[英]Pandas - After Successfully Writing to a New Workbook, Excel Gives Invalid File Error
[英]Writing out to a gives no new file, but no error either
目標:
例如:在命令行中:
$ 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參數是否不將輸入文件存儲為目的地? 嘗試寫入時是否使用文件句柄?
您永遠不會在輸出文件上調用close
。 Python的編寫在某種程度上得到了緩沖,根據文檔,如果您不調用flush
或close
,則不能保證將所有輸出都存儲在文件中(或簡稱為簡短文件)。
您應該始終使用with open() as ofile:
語法的文件IO with open() as ofile:
以確保正確刷新/關閉文件:
if options.out_arg:
with open(options.out_33, 'a') as outputfh:
outputfh.write(...)
else:
...
當然,所有這些都假設您實際上print_output
某個地方調用了 print_output
,但您的代碼並未顯示。 並且options.out_33
是相對路徑,而不是絕對路徑,否則該文件將不會出現在您期望的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.