[英]Python unrar module throws MemoryError while reading a .rar file
我編寫了以下代碼來讀取.rar
文件以檢查它們是否包含 MDF 文件。
from unrar import rarfile
def is_contain_mdf_file(filepath) -> bool:
try:
if rarfile.is_rarfile(filepath):
obj = rarfile.RarFile(filepath)
members = obj.namelist()
for member in members:
content = obj.read(member)
if content.startswith(b'MDF'):
return True
except Exception as ex:
print("Error while reading archived file %s", filepath, ex)
return False
讀取僅包含一個大小為 627MB 的 MDF 文件的 rar 文件時會多次引發以下錯誤。 但是仍然執行檢查(如果文件以“MDF”開頭)
Traceback (most recent call last):
File "_ctypes/callbacks.c", line 232, in 'calling callback function'
File ".....\python\venv37\lib\site-packages\unrar\rarfile.py", line 110, in _callback
self._data += chunk
MemoryError
為什么我會收到錯誤,我該如何防止它? 系統有足夠的 memory 來處理大文件。
操作系統:Windows 10
Python 3.7版
如果您收到意外的 MemoryError 並且您認為應該有足夠的 RAM 可用,這可能是因為您使用的是 32 位 python 安裝。
如果您有 64 位操作系統,簡單的解決方案是切換到 python 的 64 位安裝。
問題是 32 位 python 只能訪問約 4GB 的 RAM。 如果您的操作系統是 32 位,由於操作系統開銷,這可能會進一步縮小。
您可以在此處了解有關 32 位操作系統限制為 ~4GB RAM 的更多信息: https://superuser.com/questions/372881/is-there-a-technical-reason-why-32-bit-windows- is-limited-to-4gb-of-ram
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.