Editor

Predict the Read Output

Beginner · Predict the output · from Python File Handling: Read, Write, and Process Files

From the lesson: Python File Handling: Read, Write, and Process Files

Imagine you have a notebook where you write down important things. You open it, read what's inside, add new notes, and then close it. Python works with files the same way.

Read the lesson

What does this code print? Think about what readlines() returns and how len() works on a list.

import io

f = io.StringIO('a\nb\nc\nd\n')
lines = f.readlines()
print(len(lines))
f.close()
import io
f = io.StringIO('a\nb\nc\nd\n')
lines = f.readlines()
print(len(lines))
f.close()

More File Handling exercises

All Python exercises · 50 practice problems · File I/O & Data Formats quiz