Ram

Lesson 13 of 19

Modules & Files

Reuse code and work with text files.

Introduction

Modules let you split a program into focused files and import useful code from Python's standard library or your own project.

The with statement closes files automatically, even when an error occurs.

Key insight

Reuse code and work with text files.

Example

from pathlib import Path

Path("notes.txt").write_text("Keep practicing!")
print(Path("notes.txt").read_text())

This example shows the core modules & files idea in a small, runnable program.

Real-world example

Modules & Files appears in everyday application code, scripts and APIs.

Test yourself

2-question check

1

What is the main idea in Modules & Files?

2

Where can modules & files be useful?

Ready to check your understanding?

Answer every question, then submit your quiz.

Try it yourself

Python compiler

Runs in your browser. Nothing is sent to a server.

Output appears here

First run downloads the Python runtime, so it may take a moment.

Practice challenge

Use pathlib to check whether a file exists.

Previous
Next