Which is the best alternative to readline in Python?
I want to read a file-like object line by line. This file-like object contains these methods:
- read(): reads a byte string from the file-like object at the current offset.
- seek(): seeks to an offset within the file-like object.
- get_offset(): retrieves the current offset into the file-like object.
- get_size(): retrieves the size of the file-like object.
I can't read the complete text for file greater than 2GB, so I can't do something like that:
for line in file_object.read(): dostuff(line) I tested some methods to read line by line: these are fine with very small file, but with big file there are so slow. Anyway these are much slower than File readline(). This is an example that I tried:
text = '' while True: char = file_object.read(1) if char == '': return '' text += buffer + char if char == '\n': pos_newline = text.find('\n') current_offset += pos_newline + 1 buffer = text[pos_newline + 1:] line = text[:pos_newline] return line I also tried to read 10/50/100 characters at a time.
I can use only Standard Libraries.
EDIT: the "file-like" is not iterable.
.readline(). Directly iterating through file pointer is suitable in the case for line by line iteration.file_object.