Python – Copy all the content of one file to another file in uppercase
Last Updated :
17 Oct, 2021
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
f1 = open ( "sample file 1.txt" , "r" )
f2 = open ( "sample file 2.txt" , "w" )
for line in f1:
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
f1 = open ( "sample file 1.txt" , "r" )
f2 = open ( "sample file 2.txt" , "a" )
for line in f1:
f2.write(line.upper())
|
Output:
Share your thoughts in the comments
Please Login to comment...