A path is the address of a file — how a program finds your data on disk. When you call
pd.read_csv("data/invoices.csv"), that string is a path. Paths look different on Windows and on
Mac/Linux, and that difference is one of the most common reasons a script that runs fine for you breaks the
moment you email it to a colleague. The terms below are the heart of this page. Read them first, then the rest
of the page shows each one in action: the parts of a path, a path that breaks in a string, and
a portable path you build yourself.
These are the words to walk away with. Each one is demonstrated further down the page, but the definitions are the main takeaway.
data/invoices.csv.C:\Users\sean\data.csv on Windows, or
/Users/sean/data.csv on Mac and Linux. It names the full route from the root.data/invoices.csv means "the data folder next to my script." It travels
with the project, so it is the portable choice.\; Mac and Linux use a forward slash
/. This single character is what does not travel between machines..csv in
invoices.csv. It is part of the file name, not a separate piece of the path.data/invoices.csv finds different files depending on where you launch
the script.r in front, like r"C:\Users\sean". The
r turns off backslash escaping, so the backslashes stay literal instead of becoming a newline
or a tab.Path("data") / "invoices.csv" joins the
pieces with the right separator for whatever machine runs the code, which is why scripts that use it are
portable. The recommended fix on this page.A path reads left to right, from the broadest container down to the file. Watch each part light up and get named.
C:\Users\sean\data\invoices.csv
The same idea, three shapes. Absolute paths start from the very top of the disk. A relative path starts from wherever your script is running, so it travels with the project.
| Kind | Looks like | Reads as |
|---|---|---|
| Windows, absolute | C:\Users\sean\data\invoices.csv |
Drive C:, then folders, backslash \ separators. |
| Mac / Linux, absolute | /Users/sean/data/invoices.csv |
Starts at the root /, forward-slash / separators, no drive letter. |
| Relative | data/invoices.csv |
No leading slash or drive — "the data folder next to my script." Portable. |
\;
Mac and Linux use a forward slash /. A path written for one machine does not automatically
work on the other. That is the whole problem this page is about.Here is the part that surprises people. Inside a Python string, a backslash is not just a
backslash — it starts an escape sequence. So \n means newline and \t means
tab. Paste a Windows path straight into quotes and Python quietly rewrites it.
path = "C:\Users\new\table.csv" print(path)
print(path)
shows? It is not the line you typed. Look for where \n and \t sit.Here is how this bites you for real. Alex, an engineer on a Mac, mails you an analysis script with this line:
df = pd.read_csv("/Users/alex/data/invoices.csv")
You open it on your Windows laptop, run it, and it fails — there is no /Users/alex
on your machine. Send a Windows path back the other way (C:\Users\sean\...) and it breaks on
Alex's Mac too, for the backslash reasons you just saw. Sharing a hard-coded absolute path is one of the
most common ways a script fails on someone else's computer.
The fix, in order of preference:
from pathlib import Path; Path("data") / "invoices.csv" builds the path
with the right separator for whatever OS runs the script. This is the recommended fix."C:/Users/sean/data.csv" also works in Python on Windows.r"C:\Users\sean" turns off escaping when you must paste a Windows path.os.path.join("data", "invoices.csv"), the older equivalent of pathlib.Try the recommended fix yourself. Enter a folder and a file name;
the code below joins them with pathlib. Click Run to see the path it builds, the
separator it chose, and the OS this Python reports — all real, running in your browser.
pathlib put between your folder and
file — a backslash \ or a forward slash /? Pyodide is Linux under the hood, so
decide before you Run.