Should I use readinto method for python file or not?

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.

+5
source share
1 answer

You can use io.FileIO in the python standard library instead of the built-in open or file function if you are not sure about file.readinto .

Here's the docstring:

 #io.FileIO.readinto? Type: method_descriptor String form: <method 'readinto' of '_io.FileIO' objects> Docstring: readinto() -> Same as RawIOBase.readinto(). 

The io.RawIOBase.readinto document can be found here .

class io.RawIOBase

...

readinto (b)

Read up to len (b) bytes in bytearray b and return the number of bytes read. If the object is in non-blocking mode and there are no bytes available, None is returned.

It is available for both Python 2 and 3.

+2
source

Source: https://habr.com/ru/post/1240536/


All Articles