I recently met the readinto method of a readinto object (in Python 2.7), it is similar to fread in C. In some cases, it seems convenient and powerful. I plan to use it to read multiple files into one pre-allocated numpy array without copying the data .
eg.
a = np.empty(N) b = memoryview(a) fp1.readinto(b[0:100]) fp2.readinto(b[100:200])
and
fp1.readinto(b[0:100]) fp1.seek(400, 1) fp1.readinto(b[100:200])
I used Cython and fread for this before I met readinto . Therefore, I am very happy to know about a pure python solution.
However, its line of the document indicates
file.readinto? Type: method_descriptor String form: <method 'readinto' of 'file' objects> Namespace: Python builtin Docstring: readinto() -> Undocumented. Don't use this; it may go away.
Do not use this ? What happened?
So, I'm confused, should I use readinto or not? Can this cause any unwanted problem?
Is there an alternative implementation for the code above without readinto , but also avoid copying data ? (To avoid copy tools, np.concatenate or np.stack not a good choice.)
Any suggestion is welcome! Thanks.
------- upate -------
It seems that I can use io.FileIO in the standard library instead of the built-in open function. This looks fine, so I am posting it as an answer.
Any comments or other solution are still welcome!
------- upate -------
If you run into the same problem, you can take a look at the comments below: Andrea Corbellini and Padrake Cunningham.