-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
44 lines (37 loc) · 1.17 KB
/
main.py
File metadata and controls
44 lines (37 loc) · 1.17 KB
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
import sys
import os
from stats import (
word_count,
char_count,
sorted_dict
)
def get_book_text(path):
with open(path) as f:
return f.read()
def print_report(book_path, num_words, char_counts):
print("============ BOOKBOT ============")
print(f"Analyzing book found at {book_path}...")
print("----------- Word Count ----------")
print(f"Found {num_words} total words")
print("--------- Character Count -------")
for item in char_counts:
if item["char"].isalpha():
print(f"{item['char']}: {item['num']}")
print("============= END ===============")
def main():
if len(sys.argv) < 2:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
else:
book_path = sys.argv[1]
# if the file path doesnt exist it will error out.
if not os.path.exists(book_path):
print(f"Error: File '{book_path}' not found!")
sys.exit(1)
text = get_book_text(book_path)
num_words = word_count(text)
raw_char_counts = char_count(text)
char_counts = sorted_dict(raw_char_counts)
print_report(book_path, num_words, char_counts)
if __name__ == "__main__":
main()