On Mon, 2 May 2022 at 00:20, Cameron Simpson <c...@cskk.id.au> wrote: > > On 01May2022 18:55, Marco Sulla <marco.sulla.pyt...@gmail.com> wrote: > >Something like this is OK? > [...] > >def tail(f): > > chunk_size = 100 > > size = os.stat(f.fileno()).st_size > > I think you want os.fstat().
It's the same from py 3.3 > > chunk_line_pos = -1 > > pos = 0 > > > > for pos in positions: > > f.seek(pos) > > chars = f.read(chunk_size) > > chunk_line_pos = chars.rfind(b"\n") > > > > if chunk_line_pos != -1: > > break > > Normal text file _end_ in a newline. I'd expect this to stop immediately > at the end of the file. I think it's correct. The last line in this case is an empty bytes. > > if chunk_line_pos == -1: > > nbytes = pos > > pos = 0 > > f.seek(pos) > > chars = f.read(nbytes) > > chunk_line_pos = chars.rfind(b"\n") > > I presume this is because unless you're very lucky, 0 will not be a > position in the range(). I'd be inclined to avoid duplicating this code > and special case and instead maybe make the range unbounded and do > something like this: > > if pos < 0: > pos = 0 > ... seek/read/etc ... > if pos == 0: > break > > around the for-loop body. Yes, I was not very happy to duplicate the code... I have to think about it. > Seems sane. I haven't tried to run it. Thank you ^^ -- https://mail.python.org/mailman/listinfo/python-list