Python Programming - Reading and Writing Files - Discussion

Discussion Forum : Reading and Writing Files - General Questions (Q.No. 28)
28.
How can you read and print the last 5 lines from a file named "log.txt"?
with open("log.txt", "r") as file:
    lines = file.readlines()
    for line in lines[-5:]:
        print(line)
last_five_lines("log.txt")
with open("log.txt", "r") as file:
    print(file.read()[-5:])
lines = open("log.txt").readlines()[-5:]
for line in lines:
    print(line)
Answer: Option
Explanation:
Using readlines() and list slicing to get the last 5 lines.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.