-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
42 lines (31 loc) · 995 Bytes
/
main.py
File metadata and controls
42 lines (31 loc) · 995 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
33
34
35
36
37
38
39
40
41
42
import os
import sys
from stats import get_num_words, get_num_characters, get_sorted_list
if len(sys.argv) >= 2:
filepath = sys.argv[1]
if not os.path.exists(filepath):
print(f"No such file or directory: {filepath}")
sys.exit(1)
else:
print("Usage: python3 main.py <path_to_book>")
sys.exit(1)
def get_book_text(filepath):
with open(filepath) as f:
text = f.read()
return text
def main():
text = get_book_text(filepath)
word_count = get_num_words(text)
characters = get_num_characters(text)
sorted_characters = get_sorted_list(characters)
print(f"""
============ BOOKBOT ============
Analyzing book found at {filepath}...
----------- Word Count ----------
Found {word_count} total words
--------- Character Count -------""")
for character in sorted_characters:
if character.isalpha():
print(f"{character}: {sorted_characters[character]}")
print("============= END ===============")
main()