0Pricing
Python Academy · Lesson

Opening and Reading Files

Open text files and read their contents with read and readlines.

Introduction

Python's built-in open() function gives you access to the filesystem. Reading files line by line is memory-efficient for large data.

open() Basics

f = open('file.txt', 'r') opens in read mode. Modes: 'r' read, 'w' write, 'a' append, 'b' binary.
# f = open('data.txt', 'r')
# content = f.read()
# f.close()
print('open demo')

All lessons in this course

  1. Opening and Reading Files
  2. Writing and Appending to Files
  3. Using the with Statement for Files
  4. Working with File Paths using pathlib
← Back to Python Academy