Write a function in python to read the content from a text file "poem.txt" line by line and display the same on screen. Solution
Question
Write a function in python to read the content from a text file "poem.txt" line by line and display the same on screen. Solution
Solution
Sure! Here is a step-by-step solution in Python:
- Open the text file "poem.txt" using the
open()
function and assign it to a variable, let's sayfile
. - Use a
for
loop to iterate over each line in the file. - Within the loop, use the
print()
function to display each line on the screen.
Here's the code:
def read_poem():
# Step 1: Open the file
with open("poem.txt", "r") as file:
# Step 2: Read and display each line
for line in file:
print(line.strip()) # Use strip() to remove any leading/trailing whitespace
# Call the function to read and display the poem
read_poem()
Make sure the "poem.txt" file is in the same directory as your Python script.
Similar Questions
Write a function, vowelCount() in Python that counts and displays thenumber of vowels in the text file named Poem.txt
Write a function in Python to read a text file, Alpha.txt and displaysthose lines which begin with the word ‘You’
Create a text file "podar.txt" in python and ask the user towrite a single line of text by user input
Write C program to open an existing file“My.TXT” and read its content and displayit.
• Content (What is the poem about? What does it describe? Keep to recount of the content)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.