Exercise
Transform Any File Content To Uppercase
Objective
Develop a Python program that reads any file and transfers its content to another file, converting all lowercase letters to uppercase. Make sure to provide the ".py" file with your name included in a comment.
Example Python Exercise
Show Python Code
# This program reads the content of a file and writes it to another file, converting all lowercase letters to uppercase.
def convert_file_content(input_filename, output_filename):
try:
# Open the input file in read mode
with open(input_filename, 'r') as infile:
# Read the content of the file
content = infile.read()
# Convert the content to uppercase
content_uppercase = content.upper()
# Open the output file in write mode
with open(output_filename, 'w') as outfile:
# Write the uppercase content to the new file
outfile.write(content_uppercase)
print(f"Content has been successfully transferred and converted to uppercase in {output_filename}.")
except FileNotFoundError:
print(f"The file {input_filename} does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
# Example usage
input_file = "input.txt" # Replace with your input file name
output_file = "output.txt" # Output file where the uppercase content will be written
convert_file_content(input_file, output_file)
Output
If the input.txt file contains:
this is some content.
convert it to uppercase.
After running the program, the output.txt file will contain:
THIS IS SOME CONTENT.
CONVERT IT TO UPPERCASE.
Share this Python Exercise