-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
32 lines (27 loc) · 957 Bytes
/
Copy pathmain.py
File metadata and controls
32 lines (27 loc) · 957 Bytes
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
from stats import count_words, count_characters, sort_output
import sys
def check_args(args):
if len(args) != 2:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
else:
bookfile = args[1]
return bookfile
def get_book_text(file_path):
with open(file_path) as f:
file_contents = f.read()
return file_contents
def main():
bookfile = check_args(sys.argv)
file_contents = get_book_text(bookfile)
num_words = count_words(file_contents)
counted_characters_sorted = sort_output(count_characters(file_contents))
print("============ BOOKBOT ============")
print(f"Analyzing book found at {bookfile}...")
print("----------- Word Count ----------")
print(f"Found {num_words} total words")
print("--------- Character Count -------")
for p in counted_characters_sorted:
print(f"{p["char"]}: {p["num"]}")
print("============= END ===============")
main()