Given a filename to the program the program should open the file in read mode and then read the contents in a reverse order.
Let's try to understand this program
Write a Python Program to Read The Contents of a File in Reverse Order
import the os module, os module is used to read the file, Starting from the end of the file to to the start of the file.
Ask a user to enter the file name, Using the input function call and store the file name into a variable, using the open function call open the file in a read mode and get the file descriptor fd_read.
import os
filename = input(“Enter the Filename : “)
fd_read = open(filename, "r")
Get the end index of the file using the seek and tell function. The seek function is used to set the file pointer to the end of the file. Once we reach the end of the file use the tell function to get the file pointer index and store index it into a variable end.
fd_read.seek(0, os.SEEK_END)
end = fd_read.tell()
Use the for loop to iterate from the end to the start of the file. Get the index and use the seek function to set the index. Use the read function to read the contents of the file at that particular index.
To read single character from a file use read(1), as shown below
for number in range(end, -1, -1):
fd_read.seek(number, os.SEEK_SET)
print(fd_read.read(1), end="")
Once the task is done use the close function call to close the file.
fd_read.close()
Final program
==================
import os
filename = input("Enter the Filename : ")
fd_read = open(filename, "r")
fd_read.seek(0, os.SEEK_END)
end = fd_read.tell()
for number in range(end, -1, -1):
fd_read.seek(number, os.SEEK_SET)
print(fd_read.read(1), end="")
fd_read.close()
Input to the program: contents of the file sample.txt
=======================================
Hello world
How are you ???
Output of the program
======================
??? uoy era woH
dlrow olleH
Write a Python Program to Read The Contents of a File in Reverse Order Line by Line in Reverse
Take a string variable filename and ask a user to enter the following using the input function. open the file name in the read mode and get the file descriptor. use the for loop to reverse the contents of the file by converting into a list find using reversed() function. print all the lines of the file
filename = input("Enter a Filename : ")
fd_read = open(filename, "r")
This will print the lines in the reverse order fashion by below output. Finally close a file using close function.
for line in reversed(list(fd_read)):
print(line)
fd_read.close()
Final Program
===============================
filename = input("Enter a Filename : ")
fd_read = open(filename, "r")
for line in reversed(list(fd_read)):
print(line)
fd_read.close()
Input the program: contents of the sample.txt file
======================================
Hello world
How are you ???
How can I help you ?
Output of the program
=====================
Enter a Filename : sample.txt
How can I help you ?
How are you ???
Hello world
Conclusion
=================
Comment down below if you have any other method to read the contents of the file in reverse order.
0 Comments