Python: warming up...

File Paths Trainer

Built-in AI tutor. Stuck on a path? Ask the helper on this page. It coaches, it does not hand you the answer.

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.

The vocabulary of file paths

These are the words to walk away with. Each one is demonstrated further down the page, but the definitions are the main takeaway.

Path
The address of a file — the string a program uses to find your data on disk, such as data/invoices.csv.
Absolute path
A path that starts at the very top of the disk: C:\Users\sean\data.csv on Windows, or /Users/sean/data.csv on Mac and Linux. It names the full route from the root.
Relative path
A path that starts from wherever your script is running, with no leading slash or drive letter — for example data/invoices.csv means "the data folder next to my script." It travels with the project, so it is the portable choice.
Path separator
The slash between folders. Windows uses a backslash \; Mac and Linux use a forward slash /. This single character is what does not travel between machines.
Windows vs Mac / Linux
The core portability problem: a path written with backslashes for Windows does not automatically work on a Mac, and the reverse breaks on Windows too. Hard-coding one machine's path is the classic way a shared script fails.
File extension
The suffix on the file name that signals its type, like the .csv in invoices.csv. It is part of the file name, not a separate piece of the path.
Working directory
The folder a program treats as "here" while it runs. A relative path is resolved against the working directory, which is why data/invoices.csv finds different files depending on where you launch the script.
Raw string
A Python string written with an 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.
pathlib
Python's built-in module for building paths. 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.
Step 1

The parts of a path

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
drive / root separator folders file name relative path

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.

KindLooks likeReads 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.
Why this matters. Windows separates folders with a backslash \; 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.
Step 2

The backslash trap

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)
Predict first. Before you run it, what do you think print(path) shows? It is not the line you typed. Look for where \n and \t sit.
Step 3

The shared-script gotcha, and the portable fix

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:

  1. pathlibfrom 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.
  2. Forward slashes"C:/Users/sean/data.csv" also works in Python on Windows.
  3. Raw stringsr"C:\Users\sean" turns off escaping when you must paste a Windows path.
  4. os.path.joinos.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.

Task.
Predict first. Which slash will pathlib put between your folder and file — a backslash \ or a forward slash /? Pyodide is Linux under the hood, so decide before you Run.
Output
Click Run to execute your code.