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()
correction in range (-1,-len(content)-1,-1) otherwise last element will be neglected
ReplyDeleteOk, I have corrected it.
Deleteyou can just use read function then reverse it by [::-1]
ReplyDeleteYes.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )