-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccwc.py
54 lines (40 loc) · 1.84 KB
/
ccwc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import argparse
def count_lines(file_content):
return file_content.count(b'\n')
def count_words(file_content):
return len(file_content.split())
def count_bytes(file_content):
return len(file_content)
def main():
parser = argparse.ArgumentParser(description="Python clone of the wc command")
parser.add_argument('files', metavar='FILE', type=str, nargs='+',
help='File(s) to be processed')
parser.add_argument('-l', '--lines', action='store_true',
help='print the newline counts')
parser.add_argument('-w', '--words', action='store_true',
help='print the word counts')
parser.add_argument('-c', '--bytes', action='store_true',
help='print the byte counts')
args = parser.parse_args()
for file_name in args.files:
try:
with open(file_name, 'rb') as file: # Read the file in binary mode
content = file.read()
line_count = count_lines(content)
word_count = count_words(content)
byte_count = count_bytes(content)
output = []
if args.lines:
output.append(str(line_count))
if args.words:
output.append(str(word_count))
if args.bytes:
output.append(str(byte_count))
if not (args.lines or args.words or args.bytes):
output = [str(line_count), str(word_count), str(byte_count)]
output.append(file_name)
print(' '.join(output))
except FileNotFoundError:
print(f"wc: {file_name}: No such file or directory")
if __name__ == "__main__":
main()