-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
34 lines (28 loc) · 852 Bytes
/
main.py
File metadata and controls
34 lines (28 loc) · 852 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
import string
def work():
print('Enter filename ')
file = input()
with open(file, 'r') as document:
counts = dict()
text = ''
for i in document:
text = text.lower() + i
wordlist = text.split()
table = str.maketrans("", "", string.punctuation)
stripped = [w.translate(table) for w in wordlist]
print('Total words = ' + str(len(stripped)))
for word in stripped:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
for number in sorted(counts.items(), key=lambda t: t[1], reverse=True):
print(number)
def start():
try:
work()
except OSError as e:
print('Try again, maybe filename does not exist?')
start()
if __name__ == '__main__':
start()