Open In App

Python program to reverse the content of a file and store it in another file

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a text file. The task is to reverse as well as stores the content from an input file to an output file. 
This reversing can be performed in two types.  

  • Full reversing: In this type of reversing all the content gets reversed. 
     
  • Word to word reversing: In this kind of reversing the last word comes first and the first word goes to the last position. 
     

Example 1: Full Reversing 
 

Input: Hello Geeks
       for geeks!
        

Output:!skeeg rof
        skeeG olleH
        

Example 2: Word to word reversing
 

Input: 
        Hello Geeks
        for geeks!

Output:
         geeks! for
         Geeks Hello

Example 1: Full Reversing 
Text file:
 

python-reverse-file-input

 

Python




# Open the file in write mode
f1 = open("output1.txt", "w")
 
# Open the input file and get
# the content into a variable data
with open("file.txt", "r") as myfile:
    data = myfile.read()
 
# For Full Reversing we will store the
# value of data into new variable data_1
# in a reverse order using [start: end: step],
# where step when passed -1 will reverse
# the string
data_1 = data[::-1]
 
# Now we will write the fully reverse
# data in the output1 file using
# following command
f1.write(data_1)
 
f1.close()


Output:
 

python-reverse-file-output-1

Time complexity: O(n), where n is the length of the input file “file.txt”.

Auxiliary space: O(n), where n is the length of the input file “file.txt”.

Example 2: Reversing the order of lines. We will use the above text file as input.
 

Python3




# Open the file in write mode
f2 = open("output2.txt", "w")
 
 
# Open the input file again and get
# the content as list to a variable data
with open("file.txt", "r") as myfile:
    data = myfile.readlines()
 
# We will just reverse the
# array using following code
data_2 = data[::-1]
 
# Now we will write the fully reverse
# list in the output2 file using
# following command
f2.writelines(data_2)
 
f2.close()


Output:
 

python-reverse-file-output-2

 



Previous Article
Next Article

Similar Reads

Python - Copy all the content of one file to another file in uppercase
In this article, we are going to write a Python program to copy all the content of one file to another file in uppercase. In order to solve this problem, let's see the definition of some important functions which will be used: open() - It is used to open a file in various modes like reading, write, append, both read and write.write() - It is used t
2 min read
Read content from one file and write it into another file
Prerequisite: Reading and Writing to text files in Python Python provides inbuilt functions for creating, writing, and reading files. Two types of files can be handled in python, normal text files and binary files (written in binary language,0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character call
2 min read
Python Program to Reverse the Content of a File using Stack
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 l
2 min read
Python - Append content of one text file to another
Having two file names entered by users, the task is to append the content of the second file to the content of the first file with Python. Append the content of one text file to anotherUsing file objectUsing shutil moduleUsing fileinput moduleSuppose the text files file1.txt and file2.txt contain the following data. file1.txt file2.txt Append the c
3 min read
Take input from user and store in .txt file in Python
In this article, we will see how to take input from users and store it in a .txt file in Python. To do this we will use python open() function to open any file and store data in the file, we put all the code in Python try-except block. Let's see the implementation below. Stepwise Implementation Step 1: First, we will take the data from the user and
2 min read
Python program to print number of bits to store an integer and also the number in Binary format
Given an integer, the task is to write a Python program to print the number of bits to store that integer and also print the same number in Binary format. Example: Input: n = 10 Output: Number of bits to store the number: 4 Binary value: 0b1010 Input: n = 120 Output: Number of bits to store the number: 7 Binary value: 0b1111000 The Task can be done
4 min read
Python - Copy contents of one file to another file
Given two text files, the task is to write a Python program to copy contents of the first file into the second file. The text files which are going to be used are second.txt and first.txt: Method #1: Using File handling to read and append We will open first.txt in 'r' mode and will read the contents of first.txt. After that, we will open second.txt
2 min read
How Can I Make One Python File Run Another File?
In Python programming, there often arises the need to execute one Python file from within another. This could be for modularity, reusability, or simply for the sake of organization. In this article, we will explore different approaches to achieve this task, each with its advantages and use cases. Making One Python File Run Another File in PythonBel
2 min read
Python program to modify the content of a Binary File
Given a binary file that contains some sentences (space separated words), let's write a Python program to modify or alter any particular word of the sentence. Approach:Step 1: Searching for the word in the binary file. Step 2: While searching in the file, the variable “pos” stores the position of file pointer record then traverse(continue) reading
3 min read
Python program to Reverse a single line of a text file
Given a text file. The task is to reverse a single line of user's choice from a given text file and update the already existing file. Examples: Input: Hello Geeks for geeks! User choice = 1 Output: Hello Geeks geeks! for Input: This is a geek Welcome to GeeksforGeeks GeeksforGeeks is a computer science portal User choice = 0 Output: geek a is This
2 min read
three90RightbarBannerImg