macOS平台Python读取超2GB文件报错,求非分块读取的根治方案
Hey there! I get it—dealing with that 2GB file size error and relying on temporary chunked workarounds is frustrating. Let’s cut to the chase: this issue is directly tied to the signed 32-bit integer limit for file offsets in older Python versions like your 3.5.2. Here are two permanent fixes that eliminate the need for loop-based chunking:
1. Upgrade Your Python Version (Most Straightforward Root Fix)
Python 3.5.x has inherent limitations with large file handling because it uses 32-bit integers for critical file operations under the hood. Upgrading to Python 3.6 or later completely resolves this—newer versions default to 64-bit offsets, removing the 2GB barrier entirely.
For your macOS Sierra setup, you can:
- Use
pyenvto install and manage a stable newer version (like 3.9.x, which is well-supported):# Install pyenv if you haven't already brew install pyenv # Install Python 3.9.16 pyenv install 3.9.16 # Set it as the default for your current project directory pyenv local 3.9.16 - Or download the official installer from the Python website and run it directly.
Once upgraded, you can read large files normally with standard open() calls, no chunking required:
with open("large_file.bin", "rb") as f: full_content = f.read()
2. Use Memory Mapping (If You Can’t Upgrade Python Immediately)
If upgrading isn’t an option right now, the mmap module lets you map the entire file into virtual memory, bypassing the 32-bit offset limit without manual chunking. This method treats the file like a large in-memory bytearray, handled efficiently by your OS’s virtual memory system.
Here’s a ready-to-use code snippet:
import os import mmap def read_large_file_with_mmap(file_path): with open(file_path, "rb") as f: # Map the entire file (length=0 tells mmap to use the full file size) with mmap.mmap(f.fileno(), length=0, access=mmap.ACCESS_READ) as mapped_file: # Read the entire mapped content in one go return mapped_file.read() # Example usage large_file_content = read_large_file_with_mmap("your_2gb_plus_file")
This works because mmap leverages your macOS 64-bit system’s native addressing, avoiding Python 3.5.2’s 32-bit offset restriction.
内容的提问来源于stack exchange,提问作者apatsekin

