Module 7

Reading a text file

Getting data from a file.

Opening a file

To read a file, you open it, read its contents, and close it. Python's open() function opens a file. The read() method reads the contents.

PythonReading a file (conceptual).
# In a real environment you would write:
# f = open("data.txt")
# content = f.read()
# f.close()
# print(content)

In this course, Python runs in your browser, so you cannot access files on your computer directly. But the concepts are the same.

What does open() do?