Python – Copy all the content of one file to another file in uppercase
In this article, we are going to write a Python program to copy all the content of one file to another file in uppercase. In order to solve this problem, let’s see the definition of some important functions which will be used:
- open() – It is used to open a file in various modes like reading, write, append, both read and write.
- write() – It is used to write a specified text to a file.
- upper() – It is used to convert all the lowercase letters to uppercase letters of a string and finally returns it.
Method 1: Using file handling to read and write
In this method, we would be using ‘r’ mode(to read the file) in the first file and ‘w’ mode(to write in a file) in the second file. Finally, we will write the content of the first file into the second file by using the write method. We will use the upper method there to capitalize the content while writing the content into the second file.
Python
# To open the first file in read modef1 = open("sample file 1.txt", "r") # To open the second file in write modef2 = open("sample file 2.txt", "w") # For loop to traverse through the filefor line in f1: # Writing the content of the first # file to the second file # Using upper() function to # capitalize the letters f2.write(line.upper()) |
Output:

Method 2: Using file handling to read and append
In this method, we would be using ‘r’ mode(to read the file) in the first file and ‘a’ mode(to append the content) in the second file. Finally, we will write the content of the first file into the second file by using the write method. We will use the upper method there to capitalize the content while writing the content into the second file.
Python
# To open the first file in read modef1 = open("sample file 1.txt", "r") # To open the second file in append modef2 = open("sample file 2.txt", "a") # For loop to traverse through the filefor line in f1: # Writing the content of the first # file to the second file # Using upper() function # to capitalize the letters f2.write(line.upper()) |
Output:





