Python Programming - Reading and Writing Files - Discussion

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

Post your comments here:

Your comments will be displayed after verification.