Example (tested)
from pathlib import Path
base = Path("work")
base.mkdir(parents=True, exist_ok=True)
note = base / "notes.txt"
note.write_text("Order 1001 shipped\nOrder 1002 late\n", encoding="utf-8")
(base / "b.csv").write_text("x\n1\n", encoding="utf-8")
(base / "a.csv").write_text("x\n2\n", encoding="utf-8")
print(note.exists(), note.name, note.stem, note.suffix)
text = note.read_text(encoding="utf-8")
print(text.splitlines()[1])
for p in sorted(base.glob("*.csv")):
print(p.name)
print(Path("work/missing.txt").exists())
Output: True notes.txt notes .txt, then Order 1002 late, then a.csv and b.csv on separate lines, then False.
Try it now
Write a program that makes a folder called inbox, writes three small CSV files into it with write_text, and then prints the name and the file ending of every CSV in that folder, in sorted order.
ClaudeStuck? Copy a ready-made prompt for Claude
Copy this, open Claude, paste it, and Claude will walk you through the lesson step by step.
For a grown-up: Claude is for adults (18+). Copy this, paste it into Claude, and use the answer to explain the lesson to your child.
I'm learning "Python for Data: files, libraries and automation" on The Future Market. Lesson 1: "Files and folders with pathlib".
What the lesson teaches:
- from pathlib import Path gives you the Path class. It works the same way on Windows, Mac and Linux.
- Join parts with the / operator: Path("reports") / "2026" / "march". This avoids slash mistakes between systems.
- Useful parts of a path: .name (the whole file name), .stem (the name without the ending) and .suffix (the ending, such as .csv).
- p.exists() tells you if a file or folder exists.
- p.read_text(encoding="utf-8") returns a file's text. p.write_text(text, encoding="utf-8") writes text and overwrites an existing file.
- Always pass encoding="utf-8". Without it Python uses the computer's default encoding, so the same program can behave differently on different machines.
- p.mkdir(parents=True, exist_ok=True) makes a folder, makes missing parent folders too, and does not complain if the folder is already there.
The exercise I'm working on:
Write a program that makes a folder called inbox, writes three small CSV files into it with write_text, and then prints the name and the file ending of every CSV in that folder, in sorted order.
I'm stuck. Please:
1. Explain the main idea in plain, simple words with one everyday example.
2. Walk me through the exercise one small step at a time. Ask me what I've done so far before giving the next step.
3. Don't just give me the final answer. Help me do it myself, and check my work when I share it.I'm a parent helping my child (about 8 years old) with a lesson called "Files and folders with pathlib" from the course "Python for Data: files, libraries and automation".
Here's what the lesson says:
A file path is the address of a file, like a street address for a house. Instead of building the address by gluing text together, pathlib gives you a little address object that knows how to join streets, check if a house exists and list the houses on a street.
The activity:
Write a program that makes a folder called inbox, writes three small CSV files into it with write_text, and then prints the name and the file ending of every CSV in that folder, in sorted order.
Please explain this so an 8-year-old understands: short sentences, a fun everyday example, and no hard words.
Then give me 3 simple questions I can ask my child to check they understood, and a way to do the activity together, one tiny step at a time.