-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgwc.py
executable file
·88 lines (71 loc) · 2.99 KB
/
gwc.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
import argparse
import sys
def count_bytes(filename):
#function to count the number of bytes in a text
with open(filename, 'rb') as file:
content = file.read()
byte_count = len(content)
return byte_count
def count_lines(filename):
#function to count lines in a text file
with open (filename, 'r') as file:
no_of_lines = sum(1 for _ in file)
return no_of_lines
def count_words(filename):
#function to count words by whitespace
with open (filename, 'r') as file:
content = file.read()
words = content.split()
no_of_words = len(words)
return no_of_words
def count_characters(filename):
#function to count characters using utf-8 encoding to handle multi byte characters
with open (filename, 'r', encoding='utf-8') as file:
content = file.read()
char_count = len(content)
return char_count
def read_input():
return sys.stdin.read()
def main():
#user help menu
parser = argparse.ArgumentParser(description='simplified unix wc version by Gift Jeremiah')
parser.add_argument('-c', action='store_true', help='Count bytes in a text file')
parser.add_argument('-l', action='store_true', help='Count lines in a text file')
parser.add_argument('-w', action='store_true', help='Count words in a file')
parser.add_argument('-m', action='store_true', help='Count characters in a file')
parser.add_argument('filename',nargs='?', help='Name of the file')
args = parser.parse_args()
if args.filename == '-':
content = read_input()
line_count = content.count('\n')
word_count = len(content.split())
char_count = len(content)
byte_count = len(content.encode())
print(f"{byte_count} bytes, {word_count} words, {char_count} characters, {line_count} lines")
#if -c flag is passed,
if args.c:
bytes_count = count_bytes(args.filename)
kb_count = bytes_count/1024
print (f"{args.filename} is {bytes_count} bytes and roughly {kb_count} kilobyte")
#if the -l flag is passed
elif args.l:
line_count = count_lines(args.filename)
print(f"{line_count} lines in {args.filename}")
#if the =w flag is passed
elif args.w:
word_count = count_words(args.filename)
print(f"There are {word_count} words in {args.filename}")
#if the -m argument is passed
elif args.m:
character_count = count_characters(args.filename)
print(f'{args.filename} has {character_count} characters')
#setting the defualt behaivour to give every detail about the file
else: # Default behavior
word_count = count_words(args.filename)
character_count = count_characters(args.filename)
lines_count = count_lines(args.filename)
bytes_count = count_bytes(args.filename)
print(f"{bytes_count} bytes, {word_count} words, {character_count} characters, {lines_count} lines")
if __name__ == '__main__':
main()