Python files for beginners
Read and process text files in Python, search lines, and work with practice file fixtures.
Understanding files
Text files let programs work with information beyond a single input. Learn to open files, iterate over lines, remove unwanted whitespace, and filter relevant records. ZHUDDLE supplies sample files inside its Python worker for these exercises, so you can practice without uploading your own files.
with open("notes.txt", "w") as handle:
handle.write("Practice Python\n")
with open("notes.txt") as handle:
for line in handle:
print(line.strip())The first block creates a small practice file. The second reads it. Using with closes the file when the block finishes.
What you will practice
Work through eight coding missions, then test your understanding with ten multiple-choice questions. Each mission provides starter code, a hint, and feedback after you run it.
- Count lines in a file — Opening files; Reading files
- Read and exhaust a handle — Reading files
- Select sender headers — Searching through a file
- Search anywhere in a line — Searching through a file
- Choose a file safely — Letting the user choose the file name; Using try, except, and open
- Uppercase a file — Exercises: Exercise 1
- Average spam confidence — Exercises: Exercise 2
- Write a fresh text file — Writing files
Your chapter progress is saved in this browser. Complete all 18 checks to unlock a certificate at your earned practice score, and download your notebook or grade report to keep a copy.
Read, practice, and try again
For the chapter reading, see Python for Everybody: Files by Charles R. Severance (CC BY 4.0). ZHUDDLE adds its own practice activities and scoring. The browser runs Python after its first interpreter download; no local installation is required.
Start the files exercises →