Reverse the order of lines of a .txt file in Python

Learn how to reverse the order of lines of a .txt file in Python. That means, the last line of the text file will appear first and the first line will appear last.

This tutorial is a part of our Python File Handling tutorial.

In Data file handling in Python, we use two types of files, namely:

  • Text file (extension-.txt)
  • Binary file (extension-.bin)

Here, we are using .txt extension files. This program is written to emphasize the operation on the text file in Python.

Here are some important tutorials on working with text files in Python.

In this program, our main goal is to reverse the contents of the text file using Python code. This means suppose the file has the following text:

Hello, my name is Ria.

I am 19.

I love mathematics.

So after the execution of this program, the output desired would be:

I love mathematics.

I am 19.

Hello, my name is Ria.

This goal can be achieved by the following steps:

  1. Create a file and enter the content you want to operate on.
  2. Make sure that a file by that name is created. Open it and the next line character is used in the code.
  3. Now reverse the contents of the file by using another function.

Python program to reverse text file lines order

We will show you how to do this from the very beginning step by step. You can skip the file creation step if you have already a text file.

  1. Create a Text file in Python
  2. Reverse the text file contents

Part I: File Creation and input desired Data

def file_Creation():
     while True:
            line=input("\n Enter a sentence")
            choice=input("Enter more?-Y/N")
            if choice=='N':break

Part II: Reversing the file content (Line order)

def Reverse_Content():
      for i in t:


Part I

Enter a sentence Spiderman-into the spider verse is a good movie.
Enter more? Y
Enter a sentence It is animated.
Enter more? Y
Enter a sentence I watched it with my siblings.
Enter more? N

Part II

I watched it with my siblings.
It is animated.
Spiderman-into the spider verse is a good movie.


Using the user-defined function “file_creation” one can emphasize the reversal of the order of lines in a text (.txt) file. In part 1, we are creating a file and entering the desired content. I entered three lines written in green in the output.

  1. Using the memory space allocated to the variable “ofile”, we are opening/creating the file “Story.txt” in write+read mode (w+). This variable is now used in the next steps to access the file instead of the file name.
  2. The boolean True is assigned to a variable “choice”.
  3. Then using the while loop, we write the desired lines into the file using the variable “line”. The content in the variable “line” is written in the file using the command ofile .write(line).
  4. Then we enter Y or N in the variable choice depending on if we want to enter text or not. If  Y is entered then new content can be appended to the file else go to step 5.
  5. Then the file is closed using the command ofile.close().

Now you can search in the windows search bar for “story.txt”. The file will open in Notepad as follows:

Reverse the contents of a .txt file in Python (Line order)

Make the changes to make the file look like this using notepad.

reverse text file in Python

In part 2, we are reversing the order in which the contents are stored in the file. Using the variable “ofile” open the already created “Story.txt” in ready-only(r) mode.

  1. Then the variable “k” stores the list created using the readlines() command.
  2. The list stored in the variable “k” is then reversed and stored in the variable “t”.
  3. Using a for loop, we can print each line using the ‘rstrip’ method from t.
  4. Hence the content is printed in the reverse order.

One response to “Reverse the order of lines of a .txt file in Python”

  1. Sunday says:

    Hi, afternoon. In the reverse code letter I is not defined. This is stopping the code from running. The solution is to do needful to define i to put the code in the right state.

Leave a Reply

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