|
11 | 11 | parser.add_argument("-c", action="store_true", help="count characters") |
12 | 12 | args = parser.parse_args() |
13 | 13 |
|
| 14 | +total = { |
| 15 | + "lines_counter": 0, |
| 16 | + "words_counter": 0, |
| 17 | + "characters_counter": 0, |
| 18 | +} |
14 | 19 | for path in args.paths: |
15 | 20 |
|
16 | 21 | with open(path, "r") as f: |
|
20 | 25 | words_counter = len(content.split()) |
21 | 26 | characters_counter = len(content) |
22 | 27 | if not args.l and not args.w and not args.c: |
23 | | - print(f"{line_counter} {words_counter} {characters_counter} {args.path}") |
24 | | - |
| 28 | + print(f"{line_counter} {words_counter} {characters_counter} {path}") |
| 29 | + total["lines_counter"] += line_counter |
| 30 | + total["words_counter"] += words_counter |
| 31 | + total["characters_counter"] += characters_counter |
25 | 32 | else: |
26 | 33 | results = [] |
27 | 34 | if args.l: |
28 | 35 | results.append(line_counter) |
| 36 | + total["lines_counter"] += line_counter |
29 | 37 | if args.w: |
30 | 38 | results.append(words_counter) |
| 39 | + total["words_counter"] += words_counter |
31 | 40 | if args.c: |
32 | 41 | results.append(characters_counter) |
| 42 | + total["characters_counter"] += characters_counter |
33 | 43 | results = " ".join(map(str, results)) |
34 | 44 | print(results, path) |
| 45 | + |
| 46 | +if len(args.paths) > 1: |
| 47 | + if not args.l and not args.w and not args.c: |
| 48 | + print( |
| 49 | + f"{total['lines_counter']} {total['words_counter']} {total['characters_counter']} total" |
| 50 | + ) |
| 51 | + |
| 52 | + else: |
| 53 | + total_with_flags = [] |
| 54 | + |
| 55 | + if args.l: |
| 56 | + total_with_flags.append(total["lines_counter"]) |
| 57 | + if args.w: |
| 58 | + total_with_flags.append(total["words_counter"]) |
| 59 | + if args.c: |
| 60 | + total_with_flags.append(total["characters_counter"]) |
| 61 | + print(" ".join(map(str, total_with_flags))," total") |
| 62 | + |
0 commit comments