LambdaLabTM
Computer Science · Class 12 · File Handling
FilesPaths⏱️ 16 min read

Relative & Absolute Paths

A program that wants a file has to say which file. There are probably four files called marks.csv on your computer already. A path is a file's full address, and there are two ways to write one — from the top of the disk, or from wherever your program is standing right now.

1A filename is not an address

“Asha” is not an address. There are lots of Ashas. “Asha, Flat 3, Nehru Road, Pune” is an address, because following it can only ever land you in one place.

Files work the same way. Folders sit inside folders, all the way up to the top of the disk, and a path is the list of folders you walk through to reach a file — written out with a separator between each step.

On Windows
C:\Users\asha\school\marks.csv

The top is a drive letter — usually C:. The separator is a backslash \.

On Mac and Linux
/home/asha/school/marks.csv

There are no drive letters. The top is just /, called the root, and the separator is a forward slash.

Note
Same idea, different punctuation. Everything in this lesson is true on both. The examples use the Windows style because that is what most school computers run, and the widget below switches between the two so you can see they are the same path either way.

2The absolute path: start at the top

Absolute path

An absolute path gives a file's location starting from the very top of the disk — the drive letter on Windows, or / on Mac and Linux. It names every folder on the way down, so it points at the same file from anywhere.

You can tell one by looking at how it starts. If it begins with C:\ or with /, it is absolute. That first character is the promise: begin at the top, do not guess.

A postal address

An absolute path is the address you write on an envelope: house, street, city, PIN code. It works whether the letter is posted from the next street or from Delhi, because it starts from something everybody agrees on. That is its strength — and its cost is that it is long, and it stops working the moment you move house.

3The bit nobody tells you: where your program is standing

Before relative paths can make any sense, you need one idea that is completely invisible on screen. When a program runs, it is always running inside some folder. That folder is called the current working directory — the CWD, or just “where you are”.

Normally it is the folder your .py file was saved in. You can always ask:

where.py
import os

print('Python is running in this folder:')
print(os.getcwd())
Output
Python is running in this folder:
/home/admin/school/programs
Watch Out
Your output will say something else — that is the point. This program was run on a Linux computer, in a folder called programs. On a Windows machine the same two lines print something like C:\Users\asha\school\programs. getcwd() is short for get current working directory, and it exists precisely because the answer is different every time.

4The relative path: start from where you are

Relative path

A relative path gives a file's location starting from the current working directory. It does not begin with a drive letter or a /, so it means nothing on its own — it has to be read together with where the program is running.

When you write marks.csv with nothing in front of it, that is already a relative path. It means “a file called marks.csv, in whichever folder I am in”. Two special names let you move around:

.
One dot — this folder

.\marks.csv and marks.csv mean exactly the same thing. The dot is usually left out.

..
Two dots — the folder above

..\notes\syllabus.txt means “go up one folder, then down into notes”. Two dots is how you climb.

5The same file, both ways

Below is a small set of folders. Pick a file, then change the folder the program is running in and watch what happens: the absolute path does not move at all, and the relative path rewrites itself every time.

Move the program, watch the addresses
The folders on the computer
C:\Users\asha
school
programsyou are here
average.py
marks.csvyou want this
notes
syllabus.txt
photo.png
Python is running in
The file you want
Absolute path
never changes
C:\Users\asha\school\programs\marks.csv

Starts at the very top and names every folder on the way down. Change the running folder above and this line does not move.

Relative path
from programs
marks.csv

The file is inside programs (or below it), so the path just walks down. No .. needed.

Same file, both times. One address is written from the top of the disk, the other from where the program happens to be standing.
Key Takeaway
That is the whole difference, and it is worth saying in one line. An absolute path describes a file. A relative path describes a journey — and a journey depends on where you set off from.

6Watching Python resolve them

The folders in the widget are real ones, and this program was run inside programs. os.path.exists() answers “is there a file at this path?” and os.path.abspath() shows the full address a relative path stands for:

lookup.py
import os

print('marks.csv              ->', os.path.exists('marks.csv'))
print('syllabus.txt           ->', os.path.exists('syllabus.txt'))
print('../notes/syllabus.txt  ->', os.path.exists('../notes/syllabus.txt'))

print()
print('marks.csv really means:')
print(os.path.abspath('marks.csv'))

print()
print('../notes/syllabus.txt really means:')
print(os.path.abspath('../notes/syllabus.txt'))
Output
marks.csv              -> True
syllabus.txt           -> False
../notes/syllabus.txt  -> True

marks.csv really means:
/home/admin/school/programs/marks.csv

../notes/syllabus.txt really means:
/home/admin/school/notes/syllabus.txt
os.path.exists('marks.csv')

True. marks.csv sits in programs, which is the folder the program is running in, so the bare name finds it.

os.path.exists('syllabus.txt')

False — and this is the mistake everyone makes once. The file certainly exists; it is just not in this folder. A bare name only ever looks where you are standing.

os.path.exists('../notes/syllabus.txt')

True. Up one folder to school, then down into notes. Same file as the line above, found this time because the path says how to get there.

os.path.abspath('../notes/syllabus.txt')

The full address the relative path stands for. Python worked out .. for itself — notice the answer has no dots left in it.

Line two is the one to remember. “File not found” almost never means the file is missing. It means Python looked in the folder it was standing in and the file was somewhere else.

7The backslash trap

Windows separates folders with \. Python uses \ inside a string to start an escape sequence \n for a newline, \t for a tab. Put a Windows path in quotes and those two facts collide.

bad_path.py
path = "C:\new\table.txt"
print(path)
Output
C:
ew	able.txt
Watch Out
Read that output slowly. Python saw \n and made a newline, then saw \t and made a tab. What is left of new is ew, and what is left of table.txt is able.txt. No error, no warning — just a path that quietly is not the one you typed.

Sometimes you are luckier and it does not run at all, which is easier to deal with because at least you find out:

worse_path.py
path = "C:\Users\new\table.txt"
print(path)
Output
  File "worse_path.py", line 1
    path = "C:\Users\new\table.txt"
           ^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

\U starts a Unicode escape, and sers\new… is not one, so the file does not even get past the check-and-translate step. There are three ways to write a Windows path safely, and all three work:

good_path.py
Double it
"C:\\school\\marks.csv"

\\ is the escape sequence for one real backslash. Correct, but tedious to read.

Use a raw string
r"C:\school\marks.csv"

The r turns escape sequences off for that string, so every backslash stays as it is.

Use forward slashes
"C:/school/marks.csv"

Windows accepts / in paths too. Simplest of the three, and it works on every operating system.

Tip
Prefer the forward slash. All three print the right path, but "C:/school/marks.csv" is the one you cannot get wrong by miscounting backslashes, and it is the only one that also runs unchanged on a Mac or a Linux machine.

8Which should you write?

Let's Recap!
Absolute pathRelative path
Starts atThe drive or rootThe current working directory
Begins withC:\ or /A name, or . or ..
ExampleC:\Users\asha\school\marks.csvmarks.csv
LengthLongShort
If the program moves folderStill finds the fileMay not find it any more
If the whole project is copied elsewhereBreaksStill works
Use a relative path when…

the file travels with the program — the data file your project reads, the report it writes. Keep them in the same folder and write marks.csv. Copy the folder to a friend's computer, or hand it in on a pen drive, and it still runs. This is what you want for almost every school program and every project.

Use an absolute path when…

the file lives somewhere fixed and has nothing to do with your program — a log on a shared drive, a file another department drops in the same place every day. The path is long and it will not survive being moved to another computer, so it earns its keep only when the location really is fixed.

9Recap

A path is a file's address

The list of folders you walk through to reach it. \ on Windows, / on Mac and Linux.

Absolute: starts at the top

Begins with C:\ or /. Names every folder down to the file, so it points at the same file from anywhere.

Relative: starts where you are

Read from the current working directory. Written on its own it means nothing — you have to know where the program is running.

os.getcwd() tells you where that is

The current working directory is invisible on screen, which is what makes 'file not found' so confusing.

. is here, .. is up one

..\notes\syllabus.txt climbs out of this folder and back down into notes.

Backslashes in strings need care

"C:\new" hides a newline. Double them, use r"...", or just use forward slashes.

✍️ Now write these yourself
  1. 1

    Run the os.getcwd() program above from the folder your .py files live in, and read the answer.

    Hint · That folder is what every relative path in that program starts from.

  2. 2

    Write down the absolute path of any file on your computer by reading the address bar of the folder it is in.

    Hint · Click the address bar and Windows shows you the whole path as text.

  3. 3

    A program runs in school\programs and wants school\notes\photo.png. Write the relative path without looking at the widget.

    Hint · Climb out of programs first.

  4. 4

    Print "D:\new\test.txt" and see what really comes out. Then fix it three different ways.

    Hint · Count the characters in the output. There are fewer than you typed.

Quick Check

Which of these is an absolute path?

Quick Check

A program running in school\programs asks for 'syllabus.txt', which is really in school\notes. What happens?

Quick Check

What does .. mean in a path?

Quick Check

Why does print("C:\new\table.txt") print two lines?