|
1 | 1 | import argparse |
2 | 2 |
|
3 | 3 | parser = argparse.ArgumentParser( |
4 | | - prog = "wc-count words, lines, characters", |
5 | | - description = "wc shell command on python" |
| 4 | + prog="wc-count words, lines, characters", description="wc shell command on python" |
6 | 5 | ) |
7 | 6 |
|
8 | 7 | parser.add_argument("-l", action="store_true", help="Count lines") |
|
15 | 14 |
|
16 | 15 | count_total_lines = 0 |
17 | 16 | count_total_words = 0 |
18 | | -count_total_bytes = 0 |
| 17 | +count_total_bytes = 0 |
19 | 18 |
|
20 | 19 | for file_path in args.path: |
21 | 20 | with open(file_path, "r") as file: |
22 | 21 | content = file.read() |
23 | 22 |
|
24 | 23 | count_lines = content.count("\n") |
25 | 24 | count_words = len(content.split()) |
26 | | - count_bytes=len(content.encode("utf-8")) |
| 25 | + count_bytes = len(content.encode("utf-8")) |
27 | 26 |
|
28 | | - count_total_lines += count_lines |
| 27 | + count_total_lines += count_lines |
29 | 28 | count_total_words += count_words |
30 | | - count_total_bytes += count_bytes |
| 29 | + count_total_bytes += count_bytes |
31 | 30 |
|
32 | 31 | if not (args.l or args.w or args.c): |
33 | | - print (count_lines, count_words, count_bytes, file_path) |
| 32 | + print(count_lines, count_words, count_bytes, file_path) |
34 | 33 | else: |
35 | 34 | line = "" |
36 | 35 | if args.l: |
37 | 36 | line += f"{count_lines} " |
38 | | - if args.w: |
| 37 | + if args.w: |
39 | 38 | line += f"{count_words} " |
40 | | - if args.c: |
| 39 | + if args.c: |
41 | 40 | line += f"{count_bytes} " |
42 | 41 | line += f"{file_path}" |
43 | 42 | print(line) |
44 | 43 | if len(args.path) > 1: |
45 | 44 | if not (args.l or args.w or args.c): |
46 | | - print (count_total_lines, count_total_words, count_total_bytes, " total") |
| 45 | + print(count_total_lines, count_total_words, count_total_bytes, " total") |
47 | 46 | else: |
48 | 47 | line = "" |
49 | 48 | if args.l: |
50 | 49 | line += f"{count_total_lines} " |
51 | | - if args.w: |
| 50 | + if args.w: |
52 | 51 | line += f"{count_total_words} " |
53 | | - if args.c: |
| 52 | + if args.c: |
54 | 53 | line += f"{count_total_bytes} " |
55 | 54 | line += " total" |
56 | 55 | print(line) |
57 | | - |
58 | | - |
59 | | - |
60 | | - |
61 | | - |
62 | | - |
63 | | - |
0 commit comments