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

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 lowercase 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 the lowercases using lower().
				
					# Open file in read mode
with open('ReadContent.txt', 'r') as data_file:
        # Open another file in append mode
        with open('Writecontent.txt', 'a') as output_file:
            # Iterate over lines of the file
            for line in data_file:
                # Convert characters in the line to lowercases and
                # Write it to 2nd file
                output_file.write(line.lower())
				
			

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.

 

 

read a random line from a file.

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

Leave a Comment