简体   繁体   English

内存错误-Python(巨大的文件更改)

[英]Memory Error - Python (Huge File Changes)

I am trying to change the End of Line(EOL) of a file from Windows to Linux, the file size is 50 GB and writing into the same file, Below is my code : 我正在尝试将文件的行尾(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()

It gives me this error : 它给了我这个错误:

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: 

Am I missing something ? 我想念什么吗? Please help ? 请帮忙 ?

This might be because of the size of file. 这可能是由于文件的大小。 (it might be too large) (可能太大)

Reading a large file into memory all at once throws this error generally. 通常一次将大文件读入内存通常会引发此错误。

You can process this by reading the file line-by-line with below code: 您可以通过以下代码逐行读取文件来进行处理:

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()

This will solve the problem of MemoryError . 这将解决MemoryError的问题。 You can also look at Memory error due to the huge input file size 您还可以查看由于输入文件太大而导致的内存错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM