Skip to content

Computer Science · Ch 2 — File Handling in Python

Reading from a Text File

2.5

Reading from a Text File

Before you can read from a file, you must make sure it is opened in a mode that permits reading — "r", "r+", "w+", or "a+". Python then gives you three distinct ways to read the contents of a file, each suited to a different need: read(), readline(), and readlines(). The next three sections cover each of these methods, reading from the file myfile.txt — the …