简体   繁体   中英

How can I find out if a file-like object performs newline translation?

I have a library that does some kind of binary search in a seekable open file that it receives as an argument.

The file must have been opened with open(..., newline="\\n") , otherwise .seek() and .tell() might not work properly if there's newline translation.

The README of the library does make this thing clear, but still it's easy to miss. I missed it myself and I was wondering why things aren't working properly. I'd therefore like to make the library raise an error or at least a warning if it receives a file-like object that performs text translation. Is it possible to make this check?

I see two ways around this. One is Python 3.7's io.TextIOWrapper.reconfigure() (thanks @martineau!).

The second one is to make some tests to see whether seek / tell work as expected. A simple but inefficient way to do it is this:

from io import SEEK_END


def has_newlines_translated(f):
    f.seek(0)
    file_size_1 = len(f.read())
    file_size_2 = f.seek(0, SEEK_END) - 1
    return file_size_1 != file_size_2

It may be possible to do it more efficiently by reading character by character (with f.read(1) ) until past the first newline and playing with seek() / tell() to see whether results are consistent, but it's tricky and it wouldn't work in all cases (eg if the first newline is a lone \\n whereas other newlines are \\r\\n ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM