簡體   English   中英

內存錯誤-Python(巨大的文件更改)

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

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