Stage 1 · Lesson 9 of 9
Modules and reading files
Programs grow past one file, and real data lives in files. import lets you use : the that ships with Python, such as import random or from pathlib import Path, and your own .py files in the same folder. , if __name__ == "__main__":, runs code only when a file is run directly, not when another file imports it.
That keeps your reusable. For , use with open("data.txt", encoding="utf-8") as f: and loop over f line by line. The with block closes the file for you, even if an error happens halfway.
For each line, strip the newline, split it into fields, convert the types, and be ready for FileNotFoundError. This is exactly what the milestone program needs.
A question to check yourself, an AI practice session and 3 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking Python Fundamentals
Design Gurus · Beginner · $50
Beginner Python with an in-browser exercise after nearly every lesson
- CourseFreeCS50's Introduction to Programming with Python
Harvard University · Beginner
Free Harvard course whose weekly problem sets make you write real programs