Consider the following code, where big_file
is the path to a file that will not fit in memory.
f = os.open(big_file, os.O_RDONLY)
data = mmap.mmap(f, 0, access=mmap.ACCESS_READ)
At this point, data
behave like a string with slice notation or normal file operations. However, I am programming against an API which expects an object of type bytes
to be passed in, and gives the following error if I try to pass the variable data
.
TypeError: expected request_binary as binary type, got <class 'mmap.mmap'>
For small files, I cannot simply pass in data.read()
or even skip the mmap
altogether, but for large files, this will cause a MemoryError
.
Is there a way to wrap or cast the mmap
object so it can be used as bytes
by the API?
The docstring for the API you're using says right at the top:
Do not use this to upload a file larger than 150 MB. Instead, create an upload session with
files_upload_session_start
.
Assuming your computer has more than 150 MB of free memory, mmap won't help you here. You need to use the API they recommend (which supports chunked uploads where you send one piece at a time).