THE FUTURE MARKETACADEMY
Your classroomRoom 07 · Tech Lab
School of Data & Tech

Python for Data: files, libraries and automation

Step 6 of 8 in Tech Lab

Next in your path: 7.6.2 Business Websites: design, SEO and forms

What you'll be able to do
  • Work with files and folders safely using pathlib.
  • Read and write CSV and JSON, the two most common data formats.
  • Call a web API from Python, handle errors and turn the answer into data you can use.
  • Set up a project with a virtual environment and install libraries with pip.
  • Use pandas to load, clean and summarise a table of data.
  • Combine all of it into a script that processes a folder of files and writes a report, with clear messages when something goes wrong.

Before this course: Python for Beginners (course 7.5). You should already be able to run a Python file, use variables, if and for, lists and dictionaries, write functions, use try and except, and read a CSV file with the csv module. If those are new, take 7.5 first. This course does not repeat them.

Every code example in this course was run and its output checked. The pandas examples were run with pandas 3.1.1 on Python 3.11 and checked again with pandas 2.3.3 on Python 3.9, with the same results. The example files are small on purpose, so you can check the answers by hand.

Words in this course 34Open the Vocabulary Hall
01

Files and folders with pathlib

In simple words

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.

Key facts
  • 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.
  • folder.glob("*.csv") finds files that match a pattern. The results come in no guaranteed order, so wrap them in sorted(...).
  • Reading a file that is not there raises FileNotFoundError. Catch it with try and except, or check exists() first.
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.

Step by step
  1. Make a new folder for this course and open a terminal in it.
  2. Create a file called files.py and paste the example above.
  3. Run it with python files.py (Windows) or python3 files.py (Mac).
  4. Look in your folder: you should now have a folder called work with three files.
  5. Change the program to make a folder reports/2026/march with mkdir(parents=True, exist_ok=True), then run it twice. The second run must not crash.
  6. Add a try and except FileNotFoundError around reading a file that does not exist, and print a friendly message.
  7. Check: the output lines match the example, and the second run of the mkdir program does not show an error.
Watch on YouTube
Python tutorial: pathlib, the modern way to handle file pathsCorey Schafer · YouTube · youtube.com

Videos by other creators on YouTube, for reference. They are not made by or affiliated with The Future Market.

LESSON 01Files and folders with pathlib
LESSON 01 / ANIMATED LESSON / AI VOICE00:00 / 01:00
Read the video script
[Host] "Almost every data job starts with files. Let's handle them the safe way."
[Screen: from pathlib import Path, then Path("work") / "notes.txt"] "Path builds addresses. The slash joins the parts, on any computer."
[Screen: write_text then read_text with encoding utf-8] "Write text, read text. Always say utf-8, so it works on every machine."
[Screen: sorted(base.glob("*.csv")) printing a.csv and b.csv] "Glob finds every CSV in a folder. Sort the list, so the order is always the same."
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.
Open Claude
Sources
My notes
02

CSV and JSON: reading and writing data

This lesson is part of full access: every course and every level for one payment.

03

Calling a web API

This lesson is part of full access: every course and every level for one payment.

04

Libraries, pip and virtual environments

This lesson is part of full access: every course and every level for one payment.

05

pandas: load and explore a table

This lesson is part of full access: every course and every level for one payment.

06

pandas: clean and summarise

This lesson is part of full access: every course and every level for one payment.

07

Automate a repetitive task

This lesson is part of full access: every course and every level for one payment.

++

Final project

This lesson is part of full access: every course and every level for one payment.

??

Quiz

  1. How do you join folder names safely with pathlib?

  2. What does mkdir(parents=True, exist_ok=True) do?

  3. Why pass encoding="utf-8" when reading and writing text?

  4. What is the difference between json.load and json.loads?

  5. What does JSON null become in Python?

  6. Which error do you catch first, HTTPError or URLError?

  7. Why set a timeout on a web request?

  8. Where should an API key be kept?

  9. Which file lets someone rebuild your virtual environment?

  10. What does df.drop_duplicates() do?

  11. What does pd.to_numeric(col, errors="coerce") do to the word "ten"?

  12. Why pass index=False to to_csv?

EX

Final exam

This lesson is part of full access: every course and every level for one payment.

CE

Certificate

This lesson is part of full access: every course and every level for one payment.