Python Program to Reverse the Content of a File using Stack Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a file, the task is to change the content in reverse order using Stack, as well as store the lines of that file in reverse order in Python. Examples: Input: 1 2 3 4 5 Output: 5 4 3 2 1 Approach to Python Program to Reverse a Stack Using Recursion Create an empty stack. One by one push every line of the file to the stack. One by one pop each line from the stack and put them back to the file. Input File: Code ExplanationHere first we create a stack class and then initialize an array in the class providing and provide the method to an array that follows the LIFO rule of the Python stack. The LIFO rule stands for Last Input Frist Output which is the basic rule of the stack after that we use that stack to store the data of the file and after storing the data we extract the data from the stack and print the data we append in the stack will come out first and like that we get the output data of the file reversed. Python3 # Creating Stack class (LIFO rule) class Stack: def __init__(self): # Creating an empty stack self._arr = [] # Creating push() method. def push(self, val): self._arr.append(val) def is_empty(self): # Returns True if empty return len(self._arr) == 0 # Creating Pop method. def pop(self): if self.is_empty(): print("Stack is empty") return return self._arr.pop() # Creating a function which will reverse # the lines of a file and Overwrites the # given file with its contents line-by-line # reversed def reverse_file(filename): S = Stack() original = open(filename) for line in original: S.push(line.rstrip("\n")) original.close() output = open(filename, 'w') while not S.is_empty(): output.write(S.pop()+";\n") output.close() # Driver Code filename = "GFG.txt" # Calling the reverse_file function reverse_file(filename) # Now reading the content of the file with open(filename) as file: for f in file.readlines(): print(f, end="") Output: This is a World of Geeks. Welcome to GeeksforGeeks. Time complexity: O(n), where n is the number of lines in the file.Auxiliary space: O(n), where n is the number of lines in the file. Python Program to Reverse the Content of a File using Stack Comment More infoAdvertise with us M mahi_07 Follow Improve Article Tags : Python Python Programs python-file-handling Python file-handling-programs Python DSA-exercises Python-DSA +2 More Practice Tags : python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 6 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 9 min read Conditional Statements in Python 6 min read Loops in Python - For, While and Nested Loops 9 min read Python Functions 9 min read Recursion in Python 6 min read Python Lambda Functions 6 min read Python Data StructuresPython String 6 min read Python Lists 6 min read Python Tuples 6 min read Dictionaries in Python 7 min read Python Sets 10 min read Python Arrays 9 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 11 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 10 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like