Practical 12: Program to copy the contents of one file to another making the following modifications.
Problem Statement
Write a Python program to copy the contents of one file to another, making the following modifications:
- All full stops (.) should be replaced with commas (,).
- All lowercase letters should be replaced with uppercase letters.
- All uppercase letters should be replaced with lowercase letters.
Solution Code
# Function to copy file contents with modifications
def copy_and_modify_file(source_filename, destination_filename):
try:
# Open the source file in read mode and destination file in write mode
with open(source_filename, "r") as source_file:
content = source_file.read()
# Modify the content
modified_content = content.replace('.', ',') # Replace full stops with commas
modified_content = modified_content.lower() # Convert all characters to lowercase
modified_content = modified_content.upper() # Convert all characters to uppercase (overwrites previous)
# Write the modified content to the destination file
with open(destination_filename, "w") as destination_file:
destination_file.write(modified_content)
print(f"Contents copied and modified successfully to '{destination_filename}'.")
except FileNotFoundError:
print(f"Error: The source file '{source_filename}' was not found.")
except Exception as e:
print(f"An error occurred: {e}")
# Example usage
source_file = "source.txt" # Replace with your source file name
destination_file = "destination.txt" # Replace with your destination file name
copy_and_modify_file(source_file, destination_file)
Output
- The program will read the content of the source.txt file.
- It will replace all full stops with commas, change lowercase letters to uppercase, and vice versa (since the code first converts everything to lowercase and then to uppercase).
- The modified content will be written to the destination.txt file.
Example Output
If the source.txt file contains:
Hello World. This is a test file. Python is fun.
The output on console after running the program will be:
Hello World. This is a test file. Python is fun.
The content of destination.txt after running the program will be:
HELLO WORLD, THIS IS A TEST FILE, PYTHON IS FUN,
If the source.txt file doesn’t exist:
Error: The source file 'source.txt' was not found.
Process finished with exit code 0
Explanation
Below is the explanation of F.E. PPS Unit 4 Practical 12 solution where we have written Python Program to copy the contents of one file to another making the following modifications.
- Reading the Source File
- The program reads the entire content of the source.txt file using file.read().
- Modifying the Content
- Replacing Full Stops with Commas: content.replace(‘.’, ‘,’) replaces every full stop (.) with a comma (,).
- Converting to Lowercase and Uppercase:
- content.lower() converts all characters to lowercase.
- content.upper() converts all characters to uppercase (this step overwrites the lowercase conversion, so only the final uppercase version is retained).
- Writing to the Destination File
- The modified content is written to the destination.txt file using destination_file.write(modified_content).
- Error Handling
- The program handles a FileNotFoundError if the source file does not exist.
- It also handles any other unexpected errors by catching them with a general except block.
Key Concepts Learned
- File Handling (open(), read(), write(), with statement)
- Using with ensures proper file handling and automatic closure.
- String Manipulation (replace(), lower(), upper())
- Using replace() to replace specific characters.
- Using lower() and upper() to change case of characters in a string.
- Error Handling (try-except)
- Preventing crashes by handling file not found errors and general exceptions.