Copy the file’s contents after converting it to uppercase

In this python file program, Let’s consider we have readcontent.txt file with the below content. We will copy the file’s contents to another file after converting it to uppercase with the help of the below-given steps.

				
					# ReadContent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.
				
			
Steps to solve the program
  1. First, read the first file in the reading mode using with open(‘Readcontent.txt’, ‘r’) as data_file.
  2. Inside the above statement use another statement to read the second file in appending mode using with open(‘Writecontent.txt’, ‘a’) as output_file.
  3. Use a for loop to iterate over lines in the first file.
  4. Add each line to the second file by using write() after converting the characters in the line to uppercases using upper().
				
					# Open file in read mode
with open('file name', 'r') as data_file:
    # Open another file in write mode
        with open('file.txt', 'a') as output_file:
    # Iterate over lines of the file
            for line in data_file:
    # Convert characters in the line to uppercases and
    # Write it to 2nd file
                output_file.write(line.upper())
				
			

Output : Open the Writecontent.txt file to see the output

LINE1 : THIS IS INDIA.
LINE2 : THIS IS AMERICA.
LINE3 : THIS IS CANADA.
LINE4 : THIS IS AUSTRALIA.
LINE5 : THIS IS AFRICA.
LINE6 : THIS IS KOREA.
LINE7 : THIS IS GERMANY.
LINE8 : THIS IS CHINA.

 

 

copy the file’s contents to another file after converting it to lowercase.

count all the words from a file.

Leave a Comment