Python Programming - Reading and Writing Files - Discussion

Discussion Forum : Reading and Writing Files - General Questions (Q.No. 42)
42.
How can you read and print only the lines longer than 20 characters from a file named "data.txt"?
with open("data.txt", "r") as file:
    lines = file.readlines()
    for line in lines:
        if len(line) > 20:
            print(line)
print_long_lines("data.txt", 20)
with open("data.txt", "r") as file:
    print(file.read(20))
lines = open("data.txt").readlines()
for line in lines:
    if line.length() > 20:
        print(line)
Answer: Option
Explanation:
Using len() to filter and print lines longer than 20 characters.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.