簡體   English   中英

在Python中打開會創建一個空文件

[英]with open in Python creates an empty file

這件事讓我發瘋...

我使用Python來編寫文件。 出於測試目的,我將其限制為最小:

with open(save_dir + filename + '.txt', 'w+') as out_file:
    out_file.write('HI\n')

這是非常簡單的事情。 但是文件創建到了正確的目錄,但是沒有寫入任何內容。

我正在尋求幫助以嘗試調試代碼。 因為它很簡單。 我被卡住了。 這里的錯誤可能是什么? 有任何想法嗎?

如果要使用filename變量,則必須導入sys。

from sys import argv
script,input_file,output_file = argv
content = open(input_file,"r").read()
t = open(output_file,"w")
t.write(content)
t.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM