codescracker
python

Python Program to Print Contents of a File in Reverse Order



« Previous Program Next Program »

This article is created to cover some programs in Python that prints the content of a file (entered by user at run-time) in reverse order. For example, if a file say abc.txt contains:

Hello,
This is Python

And if user enters the name of file as input say abc.txt, after executing the program given below. Then the content of this file gets printed on output in reverse order. That looks like:

nohtyP si sihT
,olleH

Condition - Both file and program's source code must be saved in same folder (current directory).

Things to do Before Program

Because the program given below is used to print the content of a file (entered by user) in reverse order. Therefore we've to create a file say codescracker.txt with some content to print the content of this file in reverse order using the Python program given below. Create a file with following content:

This is a text File
The name of this file is codescracker.txt

Save the file with name codescracker.txt inside the current directory (the directory where the python program to print file's content in reverse order is saved). Here is the snapshot of the opened file, codescracker.txt:

python print content of file in reverse order

Now let's move on and create a Python program to read the content of this file and print in reverse order.

Print File's Content in Reverse Order

The question is, write a Python program that prints file's content in reverse order. The upcoming program is answer to this question:

print("Enter the Name of File: ")
fileName = input()

fileHandle = open(fileName, "r")
fileContent = ""
for content in fileHandle:
  fileContent = fileContent+content

print("\n----Content in Reverse Order----")
fileContent = fileContent[::-1]
print(fileContent)

Here is the initial output of this program's sample run:

print file content in reverse order

Now enter the name of newly created file and press ENTER key to read the file and print its content in reverse order as shown in the snapshot given below:

print content of file in reverse order python

Modified Version of Previous Program

What if user enters a file that doesn't exist in the current directory ?
What if the file entered by user is not accessible by the program ?
if any problem like these two gets occurred, then here we've another program, that is the modified version of previous program.

This program uses try-except block to handle with these errors happened while operating with the file. Let's have a look at the program and its sample run given below for clear understanding about it.

print(end="Enter File's Name: ")
fname = input()

try:
  fhand = open(fname, "r")

  fcont = ""
  for cont in fhand:
    fcont = fcont + cont

  print("\n----Content of \"" +str(fname)+ "\" in Original Order----")
  print(fcont)
  print("\n----Content of \"" +str(fname)+ "\" in Reverse Order----")
  fcont = fcont[::-1]
  print(fcont)

except IOError:
  print("\nThe file doesn't exist!")

Here is its sample run with same user input as of previous program:

print file content in reverse order program python

Here is another sample run with user input non.txt (non-existing file):

python program print file content in reverse order

Since, the file non.txt does not exist in the current directory, therefore program flow goes to except block and throws an error saying The file doesn't exist!. You can also modify the program according to your need.

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker