[英]Memory Error - Python (Huge File Changes)
我正在尝试将文件的行尾(EOL)从Windows更改为Linux,文件大小为50 GB并写入同一文件,以下是我的代码:
filename = "D:\AddressEvaluation\AddressStandardization\infu\InfutorFile.txt"
fileContents = open(filename,"r").read()
f = open(filename,"w", newline="\n")
f.write(fileContents)
f.close()
它给了我这个错误:
MemoryError Traceback (most recent call last)
<ipython-input-3-a87efb13f002> in <module>()
1 filename = "D:\AddressEvaluation\AddressStandardization\infu\InfutorFile.txt"
----> 2 fileContents = open(filename,"r").read()
3 f = open(filename,"w", newline="\n")
4 f.write(fileContents)
5 f.close()
MemoryError:
我想念什么吗? 请帮忙 ?
这可能是由于文件的大小。 (可能太大)
通常一次将大文件读入内存通常会引发此错误。
您可以通过以下代码逐行读取文件来进行处理:
f1 = open(filename,"w", newline="\n")
with open('D:\AddressEvaluation\AddressStandardization\infu\InfutorFile.txt') as f:
for fileContents in f:
f1.write(fileContents)
f1.close()
这将解决MemoryError
的问题。 您还可以查看由于输入文件太大而导致的内存错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.