Implement Word Count
Implement Word Count
def word_count(file_path):
word_counts = {}
try:
text = file.read()
words = text.split()
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
return word_counts
except FileNotFoundError:
return None
# Example usage
file_path = 'example.txt' # Replace this with the path to your text file
word_counts = word_count(file_path)
print(f"{word}: {count}")
OUTPUT