File HandlingLesson 49Free preview
Python Read Files
This lesson is part of Python · All levels
Summary
To read the file content, Python provides read(), readline(), and readlines() on file objects. read() takes the entire content or a number of characters, readline() takes one line, and readlines() returns the list of entire lines. For large files, reading per line with for is more memory-saving than loading them all at once.
Key points
- F.read() returns the entire file content as a single string; f.read(n) limits the number of characters.
- F.readline() reading one line includes the newline characters; called repeating means forward per line.
- F.readlines() returns a list containing a string, one element for each line.
- For large files use for lines in f: which read gradually without loading it all into memory.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.