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(): try: with open("DIARY.TXT", 'r') as file: for line in file: print(line.rstrip()) except FileNotFoundError: print("DIARY.TXT file not found.") # Example usage: read_diary_file()
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.