Alright today my small daily python exercise was to build a word counter. Also for the first time I started using docstrings to comment on what functions are doing, since I just learned about help() and thought I should start doing this the right way (instead of writing comments in the main function as I sometimes did before)
Here is the exercise:
Exercise: File Word Counter
Write a Python program that counts the occurrences of each word in a text file. The program should perform the following steps:
- Ask the user to input the name of a text file they want to analyze. Ensure that the file exists.
- Read the contents of the text file.
- Tokenize the text into words (split by spaces and remove punctuation).
- Count the occurrences of each unique word and store them in a dictionary.
- Display the word count for each unique word.
I wasn’t that familiar with opening files anymore (even tho I of course I did it in Harvard CS50 Python) so I used quite a bit of ChatGPT to get this done.
""" Exercise: File Word Counter Write a Python program that counts the occurrences of each word in a text file. """ import logging import os import string # Configure logging logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s: %(message)s') def get_name(): """ Ask the user to input the name of a text file they want to analyze. Ensure that the file exists. """ logging.debug(f'get_name()') while True: try: file_name = input("Enter the name of the text file: ") if os.path.exists(file_name): logging.debug(f'returning file_name') return file_name else: logging.error(f"File '{file_name}' does not exist. Please enter a valid file name.") except KeyboardInterrupt: logging.error('KeyboardInterrupt') def read_file(file_name): """ Read the contents of the text file. """ logging.debug(f'read_file()') try: with open(file_name, 'r') as file: file_content = file.read() return file_content except FileNotFoundError: logging.error(f"File '{file_name}' not found.") return None def extract_words(file_content): """ Tokenize the text into words (split by spaces and remove punctuation). """ logging.debug(f'extract_words') # Remove punctuation and convert to lowercase text = file_content.lower().translate(str.maketrans('', '', string.punctuation)) words = text.split() return words def count_words(file_words): """ Count the occurrences of each unique word and store them in a dictionary. """ logging.debug(f'count_words') word_counts = {} for word in file_words: if word in word_counts: word_counts[word] += 1 else: word_counts[word] = 1 return word_counts def display_counts(word_counts): """ Display the word count for each unique word. """ logging.debug(f'display_counts') for word, count in word_counts.items(): print(f"{word}: {count}") def main(): file_name = get_name() file_content = read_file(file_name) file_words = extract_words(file_content) word_counts = count_words(file_words) display_counts(word_counts) if __name__ == '__main__': main()
Top comments (0)