[英]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.