Python Programming - Reading and Writing Files - Discussion

Discussion Forum : Reading and Writing Files - General Questions (Q.No. 57)
57.
How can you read and print the lines with odd line numbers from a file named "numbers.txt"?
with open("numbers.txt", "r") as file:
    lines = file.readlines()
    for i in range(len(lines)):
        if i % 2 != 0:
            print(lines[i])
print_odd_lines("numbers.txt")
with open("numbers.txt", "r") as file:
    print(file.read(2))
with open("numbers.txt", "r") as file:
    for line in file:
        if line.number() % 2 != 0:
            print(line)
Answer: Option
Explanation:
Using the index to identify odd line numbers and printing corresponding lines.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.