Transform any file content to uppercase - Python Programming Exercise

In this exercise, you will develop a Python program that reads any file and transfers its content to another file, converting all lowercase letters to uppercase. This exercise is perfect for practicing file handling, string manipulation, and loops in Python. By implementing this program, you will gain hands-on experience in handling file operations, string manipulation, and loops in Python. This exercise not only reinforces your understanding of file handling but also helps you develop efficient coding practices for managing user interactions.

 Category

Managing Files

 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

 Copy 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

 More Python Programming Exercises of Managing Files

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Reversing File Content

    In this exercise, you will develop a Python program to "invert" a file. This exercise is perfect for practicing file handling, byte manipulation, and loops in ...

  •  File Encryption Tool

    In this exercise, you will develop a Python program to encode the content of a text file into a new file, transforming the text in a way that it is not easily readabl...

  •  Word Counter

    In this exercise, you will develop a Python program to count the total number of words in a given text file. This exercise is perfect for practicing file handl...

  •  BMP Dimensions with BinaryReader

    In this exercise, you will develop a Python program to read the dimensions (width and height) of a BMP file using a BinaryReader-like approach. This exercise i...

  •  Text to HTML Converter

    In this exercise, you will develop a Python program that functions as a "Text to HTML converter". This exercise is perfect for practicing file handling, string...

  •  Reverse Binary File V2

    In this exercise, you will develop a Python program to "reverse" a binary file using a "FileStream". This exercise is perfect for practicing file handling, byt...