问题
I try to load a file in memory with this:
import mmap
with open(path+fileinput+'example.txt', 'rb') as f:
fileinput = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
When I run the code the error:
AttributeError: 'module' object has no attribute 'PROT_READ'
回答1:
The PROT_READ
and PROT_WRITE
are Unix-specific. You're likely looking for:
mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
The mmap page actually has different entries for Unix/Windows version.
回答2:
I recently got the same error message with my test program mmap.py. Renaming my test program to something else (mmap_test.py) fixed the name collision that caused numpy's memmap.py to get when it executed 'import mmap'.
来源:https://stackoverflow.com/questions/13500434/loading-file-in-memory-using-python