Notebooks and your toolkit · lesson 5 of 6
Parts in this lesson
Adata/raw/files as they arrived
BPath("data") / "raw"a path built from parts
C.read_text()a file's contents

Project folders and files

Last updated

A data project is a folder that someone else can open and understand. In this lesson you'll lay out a project the way data analysts do, keep raw data untouched, and use pathlib to reach files with paths that work on any computer.

By the end of this lesson you can

  • Lay out a project with separate folders for raw data, clean data, notebooks, code and reports
  • Explain why raw data is never edited
  • Build relative paths with pathlib and read and write files with them
  • Spot an absolute path that will break on someone else's computer

A folder for everything

Data projects grow: more files arrive, notebooks multiply, charts get made. Without a plan, a folder soon holds sales.csv, sales_new.csv, sales_FINAL.csv and sales_final2.csv, and nobody knows which is which. Most analysts use a layout like this one. Step through it.

A folder tree: tindahan/, README.md, requirements.txt.tindahan/one folder per projectREADME.mdwhat this is and how to run itrequirements.txtthe packages, with versionsdata/raw/files as they arrived: never editedsales_2024.csvclean/made by code, can be remade any timesales.csvnotebooks/exploring and explaining01-first-look.ipynbsrc/code reused by several notebookscleaning.pyreports/what you share
1 of 6 Start with one folder for the project, a README.md that says what it is, and requirements.txt listing its packages.

You don't need every folder on day one. Start with data/raw/, notebooks/ and a README.md, and add the rest when you need them.

Raw data is read-only

The most important rule in the layout: never change a file in data/raw/. Not to fix a typo, not to delete a bad row, not to rename a column.

Every fix happens in code, which reads from raw and writes to clean. That way:

  • you can always go back to exactly what you were given;
  • every change is written down, in the code, with its reason (lesson 1.1, step 5);
  • when a corrected file arrives, you drop it into raw and rerun, and every fix happens again.

Some people make the raw files read-only on their computer (right-click → Properties → Read-only on Windows), so a slip can't change them.

Paths with pathlib

A path says where a file is: data/raw/sales_2024-01.csv means "in the data folder, then raw, the file sales_2024-01.csv". Python's pathlib turns paths into objects you can build and ask questions of.

This page's notebook has a small project folder with one raw file in it. Run each cell.

01-files.ipynb
  • Path("data") / "raw" joins parts with /. It uses the right separator for the computer, so the same code works on Windows (which writes \) and on a Mac.
  • .iterdir() lists what's in a folder; .exists(), .suffix and .stem answer questions about a file.
  • .read_text(encoding="utf-8") reads the whole file as text. Always say the encoding: without it, Windows may guess a different one and mangle letters like ñ.

The notebook shows PosixPath because this page's Python runs like Linux. On Windows you'll see WindowsPath. Both work the same.

From raw to clean

Here's the rule from step 2 in code: read from raw, make a change, write the result to clean. The csv module reads each row as a dictionary, so you can use column names. (pandas does this in one line, from Stage 3. It's good to see what it does for you.)

02-batangas.ipynb

The last cell lists every CSV under data. The raw file is still there, unchanged, and the new file is in clean. Delete clean and rerun, and you get it back.

mkdir(parents=True, exist_ok=True) makes the folder, and any folders above it, and doesn't complain if it's already there, so the cell can be rerun safely.

Wrong way: a path to someone else's desktop

A path that starts from the top of a drive, like C:/Users/Ana/Desktop/..., is absolute: it only works on a computer with that exact folder. Run it.

anas-notebook.ipynb
  1. Ana wrote this on her laptop. It worked for her. You have her project folder, but not her laptop.

FileNotFoundError: this computer has no C:/Users/Ana/Desktop. The fix is a relative path, starting from the project folder: Path("data/raw/sales_2024-01.csv"). Change it, and run it again. Relative paths work for anyone with the project folder, wherever they put it.

In Jupyter, a notebook's paths are relative to the folder the notebook is in. Notebooks in notebooks/ reach the data with Path("../data/raw"), where .. means "the folder above".

Naming files

A few rules save a lot of confusion:

  • No spaces or special characters. sales_2024-01.csv, not Sales Jan (final).csv. Spaces break some tools and every command line.
  • Dates as year-month-day, like 2024-01-02. Files then sort in date order, and nobody wonders whether 01-02 means January 2 or 1 February.
  • Numbers in front of notebooks, 01-first-look.ipynb, 02-clean.ipynb, in the order they run.
  • No "final". There's always another version. Let the date or a version number say which one is newest.

Check what you learned

You find a typo in a raw data file. What do you do?
Which path will work on a classmate's computer, if they have your whole project folder?
Which file name follows the naming rules?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis