Wednesday, August 17, 2022

[FIXED] Why does the next line get printed when I call readline() multiple times instead of printing the first line

Issue

Please explain how the readline() method is different from the readlines()

f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())

Solution

The readline method reads one line from the file and returns it as a string. The string returned by readline will contain the newline character at the end. The readlines method returns the contents of the entire file as a list of strings, where each item in the list represents one line of the file.



Answered By - jengelbells
Answer Checked By - David Marino (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.