Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Reading From a File

Once you’ve opened up a file, you’ll want to read or write to the file. First off, let’s cover reading a file. There are multiple methods that can be called on a file object to help you out.

read() takes a number as a parameter and reads from the file based on that number of bytes. If no argument is passed, or None or -1 is passed, then the entire file is read.

readline() takes a number as a parameter and reads at most that number of characters from the line. This continues to the end of the line and then wraps back around. If no argument is passed, or None or -1 is passed, then the entire line (or rest of the line) is read.

readlines() reads the remaining lines from the file object and returns them as a list.

Comments & Discussion

Become a Member to join the conversation.