How to Read a File

Let's learn how to read a file in python

We'll cover the following...

Python has a builtin function called open that we can use to open a file for reading. For our examples in this course, we will use a text file name “test.txt” with the following contents (don’t worry, we’ve already worked with Educative to have these files uploaded to your execution directory):

This is a test file
line 2
line 3
this line intentionally left blank

Here are a couple of examples that show how to use open for reading:

Press + to interact
handle = open("test.txt")
handle = open(r"py101book/data/test.txt", "r")

The first example ...

Access this course and 1400+ top-rated courses and projects.