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.
The top is a drive letter — usually C:. The separator is a backslash \.
There are no drive letters. The top is just /, called the root, and the separator is a forward slash.
2The absolute path: start at the top
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
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:
import os
print('Python is running in this folder:')
print(os.getcwd())Python is running in this folder: /home/admin/school/programs
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
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:
..\marks.csv and marks.csv mean exactly the same thing. The dot is usually left out.
....\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.
Starts at the very top and names every folder on the way down. Change the running folder above and this line does not move.
The file is inside programs (or below it), so the path just walks down. No .. needed.
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:
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'))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.
path = "C:\new\table.txt"
print(path)C: ew able.txt
\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:
path = "C:\Users\new\table.txt"
print(path) 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:
"C:\\school\\marks.csv"\\ is the escape sequence for one real backslash. Correct, but tedious to read.
r"C:\school\marks.csv"The r turns escape sequences off for that string, so every backslash stays as it is.
"C:/school/marks.csv"Windows accepts / in paths too. Simplest of the three, and it works on every operating system.
"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?
| Absolute path | Relative path | |
|---|---|---|
| Starts at | The drive or root | The current working directory |
| Begins with | C:\ or / | A name, or . or .. |
| Example | C:\Users\asha\school\marks.csv | marks.csv |
| Length | Long | Short |
| If the program moves folder | Still finds the file | May not find it any more |
| If the whole project is copied elsewhere | Breaks | Still works |
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.
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
The list of folders you walk through to reach it. \ on Windows, / on Mac and Linux.
Begins with C:\ or /. Names every folder down to the file, so it points at the same file from anywhere.
Read from the current working directory. Written on its own it means nothing — you have to know where the program is running.
The current working directory is invisible on screen, which is what makes 'file not found' so confusing.
..\notes\syllabus.txt climbs out of this folder and back down into notes.
"C:\new" hides a newline. Double them, use r"...", or just use forward slashes.
- 1
Run the
os.getcwd()program above from the folder your.pyfiles live in, and read the answer.Hint · That folder is what every relative path in that program starts from.
- 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
A program runs in
school\programsand wantsschool\notes\photo.png. Write the relative path without looking at the widget.Hint · Climb out of programs first.
- 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.
Which of these is an absolute path?
A program running in school\programs asks for 'syllabus.txt', which is really in school\notes. What happens?
What does .. mean in a path?
Why does print("C:\new\table.txt") print two lines?