Table of Contents
Introduction
Given a text file with some contents. The task is to reverse the texts and save the reversed text into another file.
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
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.
0 Comments