read()
File methods that read text: read() returns the whole rest of the file as one string, readline() one line and readlines() a list of lines.
In more detail
Lines keep their newline characters, so strip them before comparing. For a big file, loop over the file object instead, which reads one line at a time without loading everything into memory.
Related terms
- file object — The object returned by open() that you read from or write to, and close when finished.
- newline — The invisible character that ends a line of text, written \n in most programming languages.
- whitespace — Characters that show as blank space, such as spaces, tabs and line breaks.
- generator — A function that can pause and resume, producing a series of values one at a time with yield, each only when it is requested.
Taught in
- Files: Writing And Reading — Hands-on Python: Complete Video Course & Book
- Chapter Two In One Sitting — Hands-on Python: Complete Video Course & Book
- Composing Web Pages In Python — Hands-on Python: Complete Video Course & Book
- Paths And Files The Modern Way — Hands-on Python: Complete Video Course & Book