Q. Reading a file line by line from the beginning What if you want to read a file backward? This happens when you need to read log files. Write a program to read and display content of file from end to beginning.


Answer =


f1 = open("pathwala.txt",'r')

data = f1.readlines()
content = ""
for i in data :
    content += i+"\n"

for i in range (-1 , -len(content)-1,-1):
    print(content[ i ],end = "")
    
f1.close()


4 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. correction in range (-1,-len(content)-1,-1) otherwise last element will be neglected

    ReplyDelete
  2. you can just use read function then reverse it by [::-1]

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post