# File Paths and Folders

> How folders nest into one tree, and how a file path spells out the way down it. Then why a path that works on one machine fails on the next.

**Course:** [Operating Systems and Files](https://learn.benibix.com/learn/development-platforms)  
**Time:** 12 min

**After this lesson you can:**

- Describe the folder tree from the root down
- Tell an absolute path from a relative one
- Avoid the path mistakes that break code on other machines

## How a file path finds a file in the folder tree

A file path is the address of a file. It lists every folder from the top of the tree down to the file. On Linux and macOS, a path looks like /home/emma/notes.txt, with forward slashes. On Windows, it looks like C:\Users\Emma\notes.txt, with a drive letter and backslashes. Both spell out the same kind of route.

Folders hold files and other folders, so together they form a tree. Directory is the older word for a folder, from the command line, and it means the same thing. The top of the tree is the root directory. On Linux and macOS there is one root, written as a single slash. On Windows, each drive has its own, such as C:\.

Each user gets a home folder for their own files. On Linux it is /home/emma, and on a Mac it is /Users/emma. Many programs keep their settings in there too. Knowing where home is saves a lot of searching when a guide tells you to edit a file inside it.

A path that starts at the root is absolute. It means the same file wherever you are. A path that does not is relative, and it starts from the folder a program was started in. So data/notes.txt means a folder called data inside the current one. Two dots mean the folder one level up.

Relative paths cause a classic bug. A script opens data/prices.csv and works fine when it is run from its own folder. Run it from anywhere else, and the same line looks for a data folder that is not there. The fix is to build the path from where the script file sits, not from where it was started.

Code that writes paths by hand often breaks on another machine. A path full of backslashes means nothing on Linux, and a path to one user's home folder fails for everyone else. A path library is safer, because it builds each path the right way for the system it runs on. Spaces need care too: in a terminal, a path with a space needs quote marks.

People often think a file's name is enough to find it. Two files can share a name in two folders. The system tells them apart only by the full path. When a program says file not found, the file is usually there. The program is just looking in the wrong folder.

```python
from pathlib import Path

here = Path(__file__).parent          # the folder this script sits in
prices = here / "data" / "prices.csv"
print(prices)
# /home/emma/shop/data/prices.csv        on Linux
# C:\Users\Emma\shop\data\prices.csv     on Windows
```
Building the path from the script's own folder makes it work from anywhere. The slash joins the parts, and pathlib writes the right one for each system.

![A tree of boxes. The root, a slash, holds home and etc. Home holds emma and jack. Emma holds two files, notes.txt and photo.png. The boxes from the root to notes.txt are lit up. Under the tree, the path to that file is written out twice: /home/emma/notes.txt on Linux and macOS, and C:\Users\Emma\notes.txt on Windows.](https://learn.benibix.com/diagrams/dev-file-tree.svg)

### Example: Jack's report runs from one folder only

Jack's script opens a file in its data folder. It works every time he runs it from the project. The nightly job starts it from his home folder, and it fails with file not found.

He builds the path from the script's own folder with pathlib. The next night it runs from home, from the nightly job and from his laptop, and finds the file every time.

**Words in this lesson:** [Directory](https://learn.benibix.com/term/development/directory), [Root Directory (/)](https://learn.benibix.com/term/infrastructure/root-directory), [File Path](https://learn.benibix.com/term/development/file-path)

**Question:** A script fails with file not found, but the file is there. What is the likely cause?

**Answer:** The script was started from another folder, so its relative path points elsewhere. A relative path starts from the folder the program was started in. Start it from somewhere else, and it looks in the wrong place. Build the path from the script's own folder instead.

---

Source: https://learn.benibix.com/learn/development-platforms/file-paths-and-folders
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
