Ejercicio 5

This commit is contained in:
perro tuerto 2023-02-13 14:50:27 -08:00
parent 09bd396ac4
commit b656944daa
1 changed files with 34 additions and 0 deletions

34
exercises/ex05/ex05.py Normal file
View File

@ -0,0 +1,34 @@
# Exercise 2
# 1. cat command
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument('paths', nargs="+", type=Path)
parser.add_argument('-b', '--number-nonblank', default=False, action="store_true")
parser.add_argument('-n', '--number', default=False, action="store_true")
args = parser.parse_args()
count = 1
rest = 0
out = []
for path in args.paths:
if path.is_file():
for line in open(path, 'r').readlines():
blank = len(line.strip()) == 0 and args.number_nonblank
if blank:
out.append({"ln": 0, "txt": ''})
else:
out.append({"ln": count, "txt": line.rstrip()})
count += 1
for el in out:
line = ''
blank = len(el["txt"].strip()) == 0 and args.number_nonblank
if args.number and not blank:
total_width = len(str(out[-1]["ln"] - rest))
curr_width = len(str(el["ln"] - rest))
extra = " " * (total_width - curr_width)
line = f" {extra}{el['ln']} "
print(f"{line}{el['txt']}")