-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwo.py
More file actions
21 lines (21 loc) · 787 Bytes
/
wo.py
File metadata and controls
21 lines (21 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""wo module. Contains function: words_occur()"""
# interface functions
def words_occur():
"""words_occur() - count the occurences of words in a file."""
# Prompt user for the name of the file to use.
file_name = input("Enter the name of the file: ")
# Open the file, read it and store its words in a list.
f = open(file_name, 'r')
word_list = f.read().split()
f.close()
# Count the number of occurences of each word in the file.
occurs_dict = {}
for word in word_list:
# increment the occurences count for this word
occurs_dict[word] = occurs_dict.get(word, 0) + 1
# Print out the results.
print("File %s has %d words (%d are unique)" \
% (file_name, len(word_list), len(occurs_dict)))
print(occurs_dict)
if __name__ == '__main__':
words_occur()