Also for fun as this is written to be like
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.
See that color is added automatic(it use Rich) which make it a good looking tool.
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.