Python Program to Reverse the Content of Text File and Store it in Another File

Files in Python:

Files are identified locations on the disc where associated data is stored. They’re used to keep data in non-volatile memory for a long time (e.g. hard disk).

We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).

We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.

As a result, a file operation in Python is performed in the following order:

  • Create a new file
  • You can either read or write (perform the operations)
  • Close the file.

Given a file, the task is to reverse the content of a given file and store it in another file in Python.

Program to Reverse the Content of Text File and Store it in Another File in Python

Below is the full approach for reversing the content of a given file and storing it in another file in Python.

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open a file in write mode. In this case, we’re writing the contents into the file.
  • Make another variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open another file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Read the above file using the read() function(get the content) and store it in a variable.
  • Reverse the content of the given file using negative indexing and store it in another variable.
  • Write the above-reversed data into the reversed file using the write() function.
  • Close the reversed file(rev_file) using the close() function.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
revFile = "reversed_file.txt"
# Open a file in write mode. In this case, we're writing the contents into the file.
rev_File = open(revFile,'w')

# Make another variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open another file in read-only mode. In this case, we're simply reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
    # Read the above file using the read() function(get the content) and store it in a variable
    gvn_data = givenfilecontent.read()

# Reverse the content of the given file using negative indexing and store it in another variable.
revrse_data = gvn_data[::-1] 
# Write the above reversed data into the reversed file using the write() function
rev_File.write(revrse_data) 
# Close the reversed file(rev_file) using the close() function
rev_File.close()

Output:

skeeghcetb si siht olleh

File Content (samplefile.txt):

hello this is btechgeeks

File Content (reversed_file.txt):

skeeghcetb si siht olleh

Google Colab Images:

Files and Code: