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 get 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:

# 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() |
chevron_right
filter_none
Output:

Example 2: Word by word reversing. We will use the above text file as input.
# 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() |
chevron_right
filter_none
Output:
Recommended Posts:
- Python Program to Reverse the Content of a File using Stack
- Python - Append content of one text file to another
- Python program to modify the content of a Binary File
- Upload file and read its content in cherrypy python
- Python program to Reverse a single line of a text file
- Python | Generate random numbers within a given range and store in a list
- Python | Sort and store files with same extension
- Python - Call function from another file
- How to store a password in database?
- PyQt5 ComboBox - User entered items store at top
- Modify Numpy array to store an arbitrary length string
- PyQt5 ComboBox – User entered items store at selected position
- PyQt5 ComboBox – User entered items store after the selected position
- PyQt5 ComboBox – User entered items store before the selected position
- PyQt5 ComboBox – User entered items store alphabetical order
- How to include one CSS file in another?
- How to copy a file from one directory to another using PHP ?
- Python | Converting String content to dictionary
- response.content - Python requests
- reStructuredText | .rst file to HTML file using Python for Documentations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

