HomePythonPython Program to Capitalize First Letter of Each Word in a File

Python Program to Capitalize First Letter of Each Word in a File

Capitalizing the first letter of each word in a file is a common text manipulation task in programming. It involves processing the content of a text file, identifying the words within it, and modifying them to have their initial letters capitalized. This process can be useful for various applications, such as cleaning up text, formatting titles, or enhancing the readability of content.

Problem Statement

You are given a text file containing sentences. Your task is to write a python program that reads the content of the file, capitalizes the first letter of each word in every sentence, and then writes the modified content back to the same file.

Python Program to Capitalize First Letter of Each Word in a File

def capitalize_words_in_file(file_path):
        with open(file_path, 'r') as file:
            content = file.read()
        capitalized_content = ' '.join(word.capitalize() for word in content.split())
        with open(file_path, 'w') as file:
        print("First letter of each word capitalized and saved to the file.")
    except FileNotFoundError:
        print(f"File '{file_path}' not found.")
    except Exception as e:
        print("An error occurred:", e)

# Replace 'input.txt' with the path to your input file
file_path = 'input.txt'

How it Works

  1. Function Definition:The program starts by defining a function called capitalize_words_in_file that takes a single argument file_path, which is the path to the input file.
  2. Reading the File Content:Inside a try block, the program attempts to open the specified file (file_path) in read mode using a context manager (with statement). The content of the file is read and stored in the variable content.
  3. Capitalizing Words:The content is split into a list of words using the split() method. Then, a generator expression is used to iterate through each word in the list and capitalize the first letter of each word using the capitalize() method. The generator expression creates a new capitalized word list. Finally, the join() method is used to join the capitalized words back into a single string with spaces.
  4. Writing Modified Content:The program opens the same file (file_path) again, but this time in write mode. This will overwrite the original content of the file. The capitalized content is then written back to the file.
  5. Handling Errors:The program includes error handling using try and except blocks. If the specified file is not found, a FileNotFoundError will be caught and an error message will be displayed. Any other exceptions will be caught by the generic Exception case, and an error message with the specific exception information will be displayed.
  6. Calling the Function:At the end of the program, the function capitalize_words_in_file is called with the file_path argument set to 'input.txt'. You should replace 'input.txt' with the actual path to your input file.

Input/ Output

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...