Python Forum
program wanted for Posix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
program wanted for Posix
#4
Skaperen you should show some effort when posting these requests 💤

(May-13-2024, 08:32 AM)Gribouillis Wrote: Here is a complete script (for a single file argument). Assumes utf8 encoding of the target file
Good,to make more robust as it fail sometime on files with Unicodes on Windows.
Windows dos bad guessing:
Error:
UnicodeDecodeError: 'charmap' codec can't decode byte .....
with open(arg.file, encoding='utf-8', errors='ignore') as infile:
    print(f'==> (head) {arg.file} <==') 

Here my take on this and i use Typer for CLI application.
import typer
from collections import deque

app = typer.Typer()

@app.command()
def headtail(filename: str, head: int = 5, tail: int = 5):
    tail_deque = deque(maxlen=tail)
    head_list = []
    total_lines = 0
    try:
        with open(filename, encoding='utf-8', errors='ignore') as file:
            for i, line in enumerate(file):
                if i < head:
                    head_list.append(line)
                tail_deque.append(line)
                total_lines += 1
        if total_lines <= head + tail:
            for line in head_list:
                print(line, end='')
        else:
            for line in head_list:
                print(line, end='')
            if tail > 0:
                print("...........")
            for line in tail_deque:
                print(line, end='')
    except FileNotFoundError:
        typer.echo(f"Error: The file '{filename}' does not exist.", err=True)
    except Exception as e:
        typer.echo(f"An error occurred: {e}", err=True)

if __name__ == "__main__":
    app()
[Image: LFeJDO.png]
Compare to see that it work the same.
Output:
G:\div_code\reader_env λ python head_tail_grib.py -n 2 -m 7 contry.txt ==> (head) contry.txt <== Tokyo;35.6897 Jakarta;-6.1750 ==> (tail) contry.txt <== Nanyang;32.9987 Hangzhou;30.2500 Foshan;23.0292 Nagoya;35.1833 Taipei;25.0375 Tongshan;34.2610 Dhanbād;23.7998 G:\div_code\reader_env λ python head_tail.py contry.txt --head 2 --tail 7 Tokyo;35.6897 Jakarta;-6.1750 ............ Nanyang;32.9987 Hangzhou;30.2500 Foshan;23.0292 Nagoya;35.1833 Taipei;25.0375 Tongshan;34.2610 Dhanbād;23.7998 G:\div_code\reader_env
Update: had two "with open" need only one.
Reply


Messages In This Thread
program wanted for Posix - by Skaperen - May-11-2024, 11:54 PM
RE: program wanted for Posix - by Gribouillis - May-12-2024, 10:19 AM
RE: program wanted for Posix - by Gribouillis - May-13-2024, 08:32 AM
RE: program wanted for Posix - by snippsat - May-13-2024, 11:08 AM
RE: program wanted for Posix - by Skaperen - May-18-2024, 06:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  the posix "cut" command Skaperen 0 1,901 Oct-28-2018, 08:29 PM
Last Post: Skaperen
  program wanted in python Skaperen 2 2,762 Aug-07-2018, 12:05 AM
Last Post: Skaperen
  program wanted: diff that ignores numbers Skaperen 0 1,961 Jun-16-2018, 02:05 AM
Last Post: Skaperen
  program wanted: clean up pyc files Skaperen 6 5,833 Jun-13-2017, 05:42 PM
Last Post: snippsat
  anyone interested in a project of making python versions of various POSIX commands Skaperen 10 10,650 Oct-17-2016, 08:36 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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