![](/img/trans.png)
[英]Why does this python procedure not produce an output after I have parsed the JSON file?
[英]Why does Python produce a MemoryError when I open this file
我正在嘗試從文件中刪除空行。 我的方法是逐行讀取文件,然后將不只是換行的任何行寫入新文件。 它適用於小型文件,但是由於我不明白的原因,我在較大的文件上遇到了MemoryError。 問題文件超過1GB,但是由於我是逐行讀取的,因此我認為我在內存中不會存儲多於一行的文件。 還是我
with open(output_path, "ab+") as out_file:
with open(input_path, "rb") as in_file:
line = in_file.readline()
while line:
if line != "\n":
out_file.write(line)
line = in_file.readline()
當我將文件拆分為大塊時,它可以正常工作,但是我不願意這樣做。 我想了解這里發生了什么。 謝謝!
事實證明,問題出在代碼的其他地方。 我沒有明確關閉文件,這導致了這個問題。 感謝你的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.