python

Python 3 Script to Reverse the Content of Text File and Save it in Another Text File Full Project For Beginners


 

 

# 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()

 

# 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()

Similar Posts:

    None Found

Leave a Reply

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