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
pathliband 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.
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
rawand 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.
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(),.suffixand.stemanswer 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.)
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.
-
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, notSales 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 whether01-02means 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
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.
- The Python Standard Library: pathlib: Path, the / operator, exists, iterdir, read_text, mkdir
- The Python Standard Library: csv: DictReader and DictWriter, and why to open with newline=''
- Wilson et al., Good enough practices in scientific computing (PLOS Computational Biology, 2017): keep raw data raw, one folder per project, name files sensibly
- Broman and Woo, Data organization in spreadsheets (The American Statistician, 2018): dates as YYYY-MM-DD, and names without spaces