English

Compare and contrast read(), readline() and readlines().

Advertisements
Advertisements

Question

Compare and contrast read(), readline() and readlines().

Distinguish Between
Advertisements

Solution

Basis of Difference read() readline() readlines()
Purpose Reads the complete file or a specified number of characters Reads one line at a time Reads all lines at once
Return value A string A string containing one line A list of strings
Argument Optional size No argument normally required No argument normally required
Memory use May use large memory if the whole file is read Memory efficient May use large memory for large files
End of file Returns an empty string Returns an empty string Returns an empty list

Example: read(10) reads 10 characters, whereas repeated calls to readline() read successive lines.

shaalaa.com
  Is there an error in this question or solution?
Chapter 7: Data File Handling - EXERCISE [Page 145]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 7 Data File Handling
EXERCISE | Q 4. | Page 145
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×