Let the OS decide for you. Use the mmap module:
https://docs.python.org/3.4/library/mmap.html
It uses a database binding mechanism for your OS to display the contents of a file in RAM.
Keep in mind that the file size limit is 2 GB if you use 32-bit Python, so be sure to use the 64-bit version if you decide to go this route.
For instance:
f1 = open('input_file', 'r+b') m1 = mmap.mmap(f1.fileno(), 0) f2 = open('out_file', 'a+b') # out_file must be >0 bytes on windows m2 = mmap.mmap(f2.fileno(), 0) m2.resize(len(m1)) m2[:] = m1 # copy input_file to out_file m2.flush() # flush results
Please note that you never had to call read () functions and decide how many bytes to add to RAM. This example simply copies one file to another, but as you said in your example, you can do whatever processing you need. Please note: if the entire file is mapped to the address space in RAM, this does not mean that it was actually copied there. It will be copied piecewise, at the discretion of the OS.
source share