Python Forum
reading a file like the unix tail -f command does
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading a file like the unix tail -f command does
#1
i want to have my script read a file that another process may or may not have written more to that file extending its size. in unix, the "tail -f" command does this once it has reached EOF. i want my script to read this way. imagine the script is implementing that "tail -f" command. my script will be doing something different but still needs to continuously read a status file up to the end. what i am worried about is if there is something i need to do for this in the Python I/O logic. once a file being read reaches EOF could Python save the EOF state and give EOF each time the script tries to read that file, even if the underlying file in the system has grown? is there anything in particular my script code needs to do if i use the Python I/O interface or should i just read that file via the unix system I/O interface? this project will only be running on Linux.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I suggest to read David Beazley's presentation Generator Tricks for Systems Programmers.

On slides 74, 75, 76 there is example of tail -f implementation in Python. Following is excerpt from these slides:

A Python version of 'tail -f'

import time
import os

def follow(thefile):
    thefile.seek(0, os.SEEK_END) # End-of-file
    while True:
         line = thefile.readline()
         if not line:
             time.sleep(0.1)    # Sleep briefly
             continue
         yield line
Idea : Seek to the end of the file and repeatedly try to read new lines. If new data is written to the file, we'll pick it up.

Using our follow function

logfile  = open("access-log")
loglines = follow(logfile)

for line in loglines:
    print(line, end='')
This produces the same output as 'tail -f'
Skaperen likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
i think that excerpt tells me all i need to know ... that Python doesn't get in the way of doing this, such as setting a flag when reading get EOF that prevents further reading. well, maybe seek to SEEK_END resets it Confused
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reading and writing a image stored in a file Skaperen 4 2,251 Feb-12-2022, 04:03 AM
Last Post: Skaperen
  reading remote active log file? korenron 3 2,886 Jun-24-2021, 09:07 AM
Last Post: korenron
  reading an f-string from a file Skaperen 4 5,099 Nov-03-2019, 01:59 AM
Last Post: Skaperen
  implement function like tail -F memory777 1 1,539 Oct-21-2019, 03:47 PM
Last Post: Larz60+
  Installed Certificates command file for python 3.7, will it cause problems on mac? CosmicCliff 0 1,990 Mar-05-2019, 07:58 PM
Last Post: CosmicCliff
  unix for the beginning mage (bash basic) metulburr 1 3,418 Nov-26-2017, 05:17 PM
Last Post: heiner55
  Pay what you want - Humble Unix Book Bundle O'Reilly Larz60+ 1 4,216 Dec-03-2016, 02:33 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020