|
98 | 98 | ) |
99 | 99 |
|
100 | 100 | parser.add_argument("-l", "--line", dest="line", help="The number of lines in each file", action="store_true") |
101 | | -parser.add_argument("-w", "---word", dest="word", help="The number of words in each file", action="store_true") |
102 | | -parser.add_argument("-c", "--char", dest="char", help="The number of characters in each file") |
| 101 | +parser.add_argument("-w", "--word", dest="word", help="The number of words in each file", action="store_true") |
| 102 | +parser.add_argument("-c", "--char", dest="char", help="The number of characters in each file", action="store_true") |
103 | 103 | parser.add_argument("paths", help="The file(s)/path(s) to read from", nargs="+") |
104 | 104 |
|
105 | 105 | args = parser.parse_args() |
@@ -130,24 +130,47 @@ def counter(item): |
130 | 130 | print(f"{stats['characters']} {path}") |
131 | 131 | else: |
132 | 132 | print(f"{stats['lines']} {stats['words']} {stats['characters']} {path}") |
133 | | - elif os.path.isdir(path): |
134 | | - directory = "folder" |
135 | 133 |
|
136 | | - for file in os.listdir(directory): |
137 | | - path = os.path.join(directory, file) |
| 134 | + total_lines += stats['lines'] |
| 135 | + total_words += stats['words'] |
| 136 | + total_characters += stats['characters'] |
| 137 | + file_count += 1 |
| 138 | + |
| 139 | + elif os.path.isdir(path): |
| 140 | + for file in os.listdir(path): |
| 141 | + file_path = os.path.join(path, file) |
138 | 142 |
|
139 | | - if os.path.isfile(path): |
140 | | - with open(path, "r") as f: |
| 143 | + if os.path.isfile(file_path): |
| 144 | + with open(file_path, "r") as f: |
141 | 145 | content = f.read() |
142 | 146 | stats = counter(content) |
143 | 147 | if args.line: |
144 | | - print(f"{stats['lines']} {path}") |
| 148 | + print(f"{stats['lines']} {file_path}") |
145 | 149 | elif args.word: |
146 | | - print(f"{stats['words']} {path}") |
| 150 | + print(f"{stats['words']} {file_path}") |
147 | 151 | elif args.char: |
148 | | - print(f"{stats['characters']} {path}") |
| 152 | + print(f"{stats['characters']} {file_path}") |
149 | 153 | else: |
150 | | - print(f"{stats['lines']} {stats['words']} {stats['characters']} {path}") |
| 154 | + print(f"{stats['lines']} {stats['words']} {stats['characters']} {file_path}") |
| 155 | + |
| 156 | + total_lines += stats['lines'] |
| 157 | + total_words += stats['words'] |
| 158 | + total_characters += stats['characters'] |
| 159 | + file_count += 1 |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +if file_count > 1: |
| 166 | + if args.line: |
| 167 | + print(f"{total_lines} total") |
| 168 | + elif args.word: |
| 169 | + print(f"{total_words} total") |
| 170 | + elif args.char: |
| 171 | + print(f"{total_characters} total") |
| 172 | + else: |
| 173 | + print(f"{total_lines} {total_words} {total_characters} total") |
151 | 174 |
|
152 | 175 |
|
153 | 176 |
|
|
0 commit comments