Program to reverse the content of text file and store it in another file using Python

by | Aug 3, 2021 | Python Programs

Home » Python » Python Programs » Program to reverse the content of text file and store it in another file using Python

Introduction

Given a text file with some contents. The task is to reverse the texts and save the reversed text into another file.

Program to reverse the content of text file and store it in another file using Python

Program

writeFile = open("updated.txt", "w") 
with open("first.txt", "r") as readFile: 
    txt = readFile.read() 
reversedContent = txt[::-1] 
writeFile.write(reversedContent) 
writeFile.close()

Output

Program to reverse the content of text file and store it in another file using Python Output

Explanation

Approach:

  • Open the read file in read mode and write file in write mode.
  • Read the contents of the file.
  • Reverse the content using [start: end: step] where giving step = -1 is used to reverse the string.
  • Write the reversed string to output file.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author