|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +from tabulate import tabulate |
| 6 | + |
| 7 | + |
| 8 | +def parse_args(): |
| 9 | + parser = argparse.ArgumentParser( |
| 10 | + description="word, line and byte count", |
| 11 | + ) |
| 12 | + parser.add_argument("paths", nargs="+", |
| 13 | + help="The file path(s) to process.") |
| 14 | + parser.add_argument( |
| 15 | + "-l", |
| 16 | + "--lines", |
| 17 | + action="store_true", |
| 18 | + help="The number of lines in each input file is written to the standard output.", |
| 19 | + ) |
| 20 | + parser.add_argument( |
| 21 | + "-w", |
| 22 | + "--words", |
| 23 | + action="store_true", |
| 24 | + help="The number of words in each input file is written to the standard output.", |
| 25 | + ) |
| 26 | + parser.add_argument( |
| 27 | + "-c", |
| 28 | + "--bytes", |
| 29 | + action="store_true", |
| 30 | + dest="bytes", |
| 31 | + help="The number of bytes in each input file is written to the standard output.", |
| 32 | + ) |
| 33 | + return parser.parse_args() |
| 34 | + |
| 35 | + |
| 36 | +def main(): |
| 37 | + args = parse_args() |
| 38 | + |
| 39 | + try: |
| 40 | + file_paths = args.paths |
| 41 | + results = {} |
| 42 | + |
| 43 | + for file_path in file_paths: |
| 44 | + stats = os.stat(file_path) |
| 45 | + count = {"lines": 0, "words": 0, "bytes": stats.st_size} |
| 46 | + |
| 47 | + with open(file_path, "r", encoding="utf-8") as file: |
| 48 | + for line in file: |
| 49 | + count["lines"] += 1 |
| 50 | + trimmed = line.strip() |
| 51 | + if len(trimmed) > 0: |
| 52 | + count["words"] += len(trimmed.split()) |
| 53 | + |
| 54 | + results[file_path] = count |
| 55 | + |
| 56 | + if len(file_paths) > 1: |
| 57 | + total = {"lines": 0, "words": 0, "bytes": 0} |
| 58 | + for file_count in results.values(): |
| 59 | + total["lines"] += file_count["lines"] |
| 60 | + total["words"] += file_count["words"] |
| 61 | + total["bytes"] += file_count["bytes"] |
| 62 | + results["total"] = total |
| 63 | + |
| 64 | + no_options_provided = not (args.lines or args.words or args.bytes) |
| 65 | + selected_option_keys = [] |
| 66 | + |
| 67 | + if args.lines: |
| 68 | + selected_option_keys.append("lines") |
| 69 | + if args.words: |
| 70 | + selected_option_keys.append("words") |
| 71 | + if args.bytes: |
| 72 | + selected_option_keys.append("bytes") |
| 73 | + |
| 74 | + output_columns = [ |
| 75 | + "lines", "words", "bytes"] if no_options_provided else selected_option_keys |
| 76 | + rows = [] |
| 77 | + for name, values in results.items(): |
| 78 | + rows.append([name] + [values[column] for column in output_columns]) |
| 79 | + |
| 80 | + if no_options_provided: |
| 81 | + print(tabulate(rows, headers=[ |
| 82 | + "index"] + output_columns)) |
| 83 | + else: |
| 84 | + print(tabulate(rows, headers=[ |
| 85 | + "index"] + output_columns)) |
| 86 | + except OSError as err: |
| 87 | + print(str(err), file=sys.stderr) |
| 88 | + |
| 89 | + return 0 |
| 90 | + |
| 91 | + |
| 92 | +main() |
0 commit comments