Python Forum
What does .flush do? How can I change this to write to the file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does .flush do? How can I change this to write to the file?
#4
Also for fun as this is written to be like tail -f which is command line tool.
Then can write to be a command line tool,eg i think Typer is cool.
So Typer is tool like eg argparse in standard library.
# py_tail.py
import typer
from collections import deque
import time

app = typer.Typer()

def follow_file(filename: str) -> None:
    with open(filename, 'r') as fh:
        # Go to the end of the file
        fh.seek(0, 2)
        while True:
            line = fh.readline()
            if not line:
                time.sleep(1)  # Wait a bit for new content
                continue
            print(line.rstrip('\n'))

@app.command()
def tail(
    filename: str,
    n: int = typer.Option(10, "--number", "-n", help="The last n lines of file"),
    follow: bool = typer.Option(False, "--follow", "-f", help="Show data as the file grows.")
    ):
    """
    Print the last n lines from a file. Optionally follow the file for new lines.
    """
    lines = deque(maxlen=n)
    try:
        with open(filename) as fh:
            for line in fh:
                lines.append(line.rstrip('\n'))
        for line in lines:
            print(line)
    except FileNotFoundError:
        typer.echo(f"File not found: {filename}")
        raise typer.Exit(code=1)
    if follow:
        follow_file(filename)

if __name__ == "__main__":
    app()
Now it's a command line tool and have also added eg tail -n 5 show the n lines of the file.
See that color is added automatic(it use Rich) which make it a good looking tool.
[Image: qUXPoG.png]
Reply


Messages In This Thread
RE: What does .flush do? How can I change this to write to the file? - by snippsat - Apr-22-2024, 01:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 506 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,672 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,630 Nov-09-2023, 10:56 AM
Last Post: mg24
  logging: change log file permission with RotatingFileHandler erg 0 1,126 Aug-09-2023, 01:24 PM
Last Post: erg
  How can I change the uuid name of a file to his original file? MaddoxMB 2 1,005 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  How do I read and write a binary file in Python? blackears 6 7,358 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,163 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,725 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 1,001 Feb-15-2023, 05:34 PM
Last Post: zsousa
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,493 Jan-21-2023, 06:57 AM
Last Post: jacklee26

Forum Jump:

User Panel Messages

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