Python Programming - Reading and Writing Files - Discussion

Discussion Forum : Reading and Writing Files - General Questions (Q.No. 46)
46.
How can you read and print the lines that end with "!" from a file named "text.txt"?
with open("text.txt", "r") as file:
    lines = file.readlines()
    for line in lines:
        if line.endswith("!"):
            print(line)
print_lines_with_end("text.txt", "!")
with open("text.txt", "r") as file:
    print(file.read("!"))
lines = open("text.txt").readlines()
for line in lines:
    if line.end() == "!":
        print(line)
Answer: Option
Explanation:
Using endswith() to filter and print lines ending with "!".
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.