tail

Barry barry at barrys-emacs.org
Sat May 7 10:08:14 EDT 2022



> On 7 May 2022, at 14:24, Marco Sulla <Marco.Sulla.Python at gmail.com> wrote:
> 
> On Sat, 7 May 2022 at 01:03, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>> 
>>        Windows also uses <cr><lf> for the EOL marker, but Python's I/O system
>> condenses that to just <lf> internally (for TEXT mode) -- so using the
>> length of a string so read to compute a file position may be off-by-one for
>> each EOL in the string.
> 
> So there's no way to reliably read lines in reverse in text mode using
> seek and read, but the only option is readlines?

You need to handle the file in bin mode and do the handling of line endings and encodings yourself. It’s not that hard for the cases you wanted.
Figure out which line ending is in use from the CR LF, LF, CR.
Once you have a line decode it before returning it.

The only OS I know that used CR was Classic Mac OS.
If you do not care about that then you can split on NL and strip any trailing CR.

Barry


> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list