Knowee
Questions
Features
Study Tools

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

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Sure! Here is a step-by-step solution in Python:

  1. Open the text file "poem.txt" using the open() function and assign it to a variable, let's say file.
  2. Use a for loop to iterate over each line in the file.
  3. 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.

This problem has been solved

Solution 2

Sure! Here is a step-by-step solution in Python:

  1. Open the text file "poem.txt" using the open() function and assign it to a variable, let's say file.
  2. Use a for loop to iterate over each line in the file.
  3. 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.

This problem has been solved

Similar Questions

Write a function in Python to read a text file, Alpha.txt and displaysthose lines which begin with the word ‘You’

Write a function, vowelCount() in Python that counts and displays thenumber of vowels in the text file named Poem.txt

Write a Python program to read a file line by line store it into an array.

Copy contents of one file to another fileWrite a Python program that copies the content from one file to another file.Note: Take the input file name from the user.Aim:To create a Python program that reads a text file, copies its contents to another file, and prints the contents to the console.Algorithm:Step 1: Accept a filename input from the user and store it in the variable file.Step 2: Open the input file (file) in read mode and the output file ('OutputData3.txt') in write mode using the open() function.Step 3: Iterate through each line in the input file (fin).Write each line to the output file (fout).Step 4: Close both the input and output files using the close() method.Step 5: Open the output file ('OutputData3.txt') in read mode and store it in the variable fin.Step 6: Iterate through each line in the output file (fin).Print each line to the console.Step 7: Close the output file (fin).Step 8: End the program.Sample Test CasesTest Case 1:Expected Output:Enter·file·name:·InputData1.txtThis·is·this.That·is·this.·That·is·that.·This·is·That.Now·tell·me·what·is·that?And·what·is·this?Confused?Haha!!Test Case 2:Expected Output:Enter·file·name:·InputData2.txtIn·a·coastal·town,·a·lighthouse·stood·tall,holding·a·magical·secret.·The·lighthousewhispered·stories·to·the·sea,·guidinglost·ships·and·providing·comfort·toweary·sailors.

Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in python sort() order as shown in the desired output.

1/2

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.