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

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

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.