|
12 | 12 |
|
13 | 13 | args = parser.parse_args() |
14 | 14 |
|
| 15 | + |
| 16 | +count_total_lines = 0 |
| 17 | +count_total_words = 0 |
| 18 | +count_total_bytes = 0 |
| 19 | + |
15 | 20 | for file_path in args.path: |
16 | | - with open(file_path, "r") as f: |
17 | | - content = f.readlines() |
| 21 | + with open(file_path, "r") as file: |
| 22 | + content = file.read() |
18 | 23 |
|
19 | | - text = "".join(content) |
20 | | - count_lines = len(content) |
21 | | - count_words = len(text.split()) |
22 | | - count_bytes=len(text.encode("utf-8")) |
| 24 | + count_lines = content.count("\n") |
| 25 | + count_words = len(content.split()) |
| 26 | + count_bytes=len(content.encode("utf-8")) |
| 27 | + |
| 28 | + count_total_lines += count_lines |
| 29 | + count_total_words += count_words |
| 30 | + count_total_bytes += count_bytes |
23 | 31 |
|
24 | 32 | if not (args.l or args.w or args.c): |
25 | 33 | print (count_lines, count_words, count_bytes, file_path) |
26 | | - if args.l: |
27 | | - print (count_lines, file_path) |
28 | | - if args.w: |
29 | | - print (count_words, file_path) |
30 | | - if args.c: |
31 | | - print (count_bytes, file_path) |
| 34 | + else: |
| 35 | + line = "" |
| 36 | + if args.l: |
| 37 | + line += f"{count_lines} " |
| 38 | + if args.w: |
| 39 | + line += f"{count_words} " |
| 40 | + if args.c: |
| 41 | + line += f"{count_bytes} " |
| 42 | + line += f"{file_path}" |
| 43 | + print(line) |
| 44 | +if len(args.path) > 1: |
| 45 | + if not (args.l or args.w or args.c): |
| 46 | + print (count_total_lines, count_total_words, count_total_bytes, " total") |
| 47 | + else: |
| 48 | + line = "" |
| 49 | + if args.l: |
| 50 | + line += f"{count_total_lines} " |
| 51 | + if args.w: |
| 52 | + line += f"{count_total_words} " |
| 53 | + if args.c: |
| 54 | + line += f"{count_total_bytes} " |
| 55 | + line += " total" |
| 56 | + print(line) |
32 | 57 |
|
33 | 58 |
|
34 | 59 |
|
|
0 commit comments