Editor

Write and Read Back

Intermediate · Write the code · 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

Create a StringIO object, write three lines to it ('Python\n', 'Java\n', 'JavaScript\n'), then seek back to the start and read all the content. Print the content using print(content, end='') to avoid an extra blank line.

import io
# Create a StringIO, write three lines, seek to start, read and print

More File Handling exercises

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