Write a method in Python to read the content from a text file DIARY.TXT line by line and display the same on the screen.

Python method called read_diary_file that reads the content from a text file named “DIARY.TXT” line by line and displays it on the screen:

def read_diary_file():
        with open("DIARY.TXT", 'r') as file:
            for line in file:

    except FileNotFoundError:
        print("DIARY.TXT file not found.")

# Example usage:


The read_diary_file method opens the file “DIARY.TXT” in read mode ('r') and iterates over each line using a for loop. The rstrip method is used to remove the trailing newline character from each line before printing it to the screen.

Please make sure that the “DIARY.TXT” file exists in the same directory as the Python script, or provide the correct path to the file if it is located in a different directory.

When you call the read_diary_file method, it will read the content of the file line by line and display it on the screen.


Leave a Reply

Your email address will not be published. Required fields are marked *