Paths in open()
open() has two things inside the brackets. The second one, the mode, gets all the attention. The first one is where programs actually go wrong. It is not just a name — it is an address, and Python looks exactly where the address says.
open().1Two kinds of address
The string you write inside open() is one of two things.
open('notes.txt')Just the name, or a short path. It means “near my program”. Move the whole folder to another computer and it still works.
open('C:/school/data/notes.txt')The whole way from the drive. It always means the same file — on this computer. On another one there may be no C:/school at all.
2Where a plain filename is looked for
A plain name like 'notes.txt' means in my own folder — the folder your program is running from. Python does not go hunting through the rest of the computer. If the file is not in that folder, it says so.
Take this arrangement of folders:
school
├── programs
│ ├── finding.py ← the program is here
│ └── marks.csv
└── data
└── notes.txtThe program is in programs. So 'marks.csv' is found and 'notes.txt' is not — it exists, but it is in the other folder:
# marks.csv is in this folder. notes.txt is in the data folder next door.
f = open('marks.csv', 'r')
print('marks.csv -> opened')
f.close()
try:
f = open('notes.txt', 'r')
print('notes.txt -> opened')
f.close()
except FileNotFoundError:
print('notes.txt -> not in this folder')
f = open('../data/notes.txt', 'r')
print('../data/notes.txt -> opened')
f.close()marks.csv -> opened notes.txt -> not in this folder ../data/notes.txt -> opened
open('marks.csv')Found. It sits beside the program, and a plain name means 'beside the program'.
open('notes.txt')Not found — FileNotFoundError. The file is real, but it is in data, and this address never says so.
open('../data/notes.txt')Found. Two dots go up to school, then the path walks down into data. Same file as the line above, reached this time because the address says how to get there.
The program is finding.py, inside programs. Tap an address.
openedJust a name, so Python looks in the folder the program is running from — programs. marks.csv is right there.
3Going down a folder, and going up
A slash means one step. A name after a slash is a step down into that folder. Two dots are a step up, out of the folder you are in.
.. is the back button
.. is that button, written down. Press it twice and you get ../../, which is two folders back.'notes.txt'In this very folder.'data/notes.txt'Down into the data folder, which is inside this one.'data/2026/notes.txt'Down twice. Every slash is one more step in.'../notes.txt'Back one folder, then the file. .. always means the folder above this one.'../../notes.txt'Back twice. Press the back button again, and again.'../data/notes.txt'Back one folder, then down into data. This is how you reach a folder standing next to yours.4Writing a Windows path: three ways that work
Windows writes folders with a backslash: C:\school\notes.txt. Python already uses the backslash inside a string for something else — \n means a new line and \t means a tab. So a Windows path typed straight into quotes turns into something you did not ask for:
bad = "C:\new\table.txt"
print(bad)
print(len(bad))C: ew able.txt 14
\n turned into a new line and \t turned into a tab. The string is only 14 characters long, not the 19 you typed. open(bad) would then say the file does not exist — and you would go looking in a folder that was never the problem.Here are three ways to write it safely. All three are correct.
"C:\\school\\notes.txt"Two backslashes in a string mean one real backslash. It works, but it is easy to miscount them.
r"C:\school\notes.txt"This is a raw string. The r switches off \n and \t for that string, so every backslash stays as you typed it.
"C:/school/notes.txt"Windows understands / in a path perfectly well. Nothing to escape, nothing to count.
The first two give exactly the same string. The last line proves it:
doubled = "C:\\school\\data\\notes.txt"
raw = r"C:\school\data\notes.txt"
forward = "C:/school/data/notes.txt"
print(doubled)
print(raw)
print(forward)
print(doubled == raw)C:\school\data\notes.txt C:\school\data\notes.txt C:/school/data/notes.txt True
r"C:\school\" does not run at all. Even in a raw string, that last backslash protects the quote after it, so Python never sees the string end. Leave the last backslash off, or use forward slashes.5“File not found” — four things to check
That is what a plain name means. If the file is anywhere else, the address has to say so — with a folder name, or with .. to go up.
Windows hides the .txt on file names, so a file shown as notes may really be notes.txt — or, if you saved it from Notepad, notes.txt.txt.
Print the path on its own line before opening it. If what comes out is not what you typed, that is the bug.
'r' and 'r+' never create a file. If you wanted a new one, you wanted 'w' or 'a'.
And a program that reads a file someone else chose should never fall over with a traceback. Catch the error and say something useful:
try:
f = open('notes.txt', 'r')
print(f.read())
f.close()
except FileNotFoundError:
print('notes.txt is not in this folder.')notes.txt is not in this folder.
6Which kind should you write?
| Relative path | Absolute path | |
|---|---|---|
| Looks like | 'notes.txt', 'data/notes.txt' | 'C:/school/notes.txt' |
| Starts from | the folder the program is in | the drive |
| Length | short | long |
| Copy the folder to another computer | still works | breaks |
| Use it for | the data file that goes with your program | a file that always lives in one fixed place |
7Recap
Not just a name. Python looks exactly where it says, and nowhere else.
The folder the program is running from. A file anywhere else needs a longer address.
.. is the back button of File Explorer, written down. '../data/notes.txt' goes back one folder and then down into data.
"C:\new\table.txt" is 14 characters and points nowhere.
Double each backslash, put an r before the quote, or use forward slashes. The forward slash is the easiest.
Wrong folder, wrong spelling, or a backslash Python read as something else.
.., folders inside folders, the three ways to write C:\school\notes.txt — is here so that you understand what open() is doing with that first string. You will rarely have to write any of it. In the board paper, and in your practicals, the answer is almost always the plain name:f = open('story.txt', 'r')That one line says: open the file called story.txt that is kept in the same folder as this Python program. Save your .py file and your data file side by side in one folder, and there is no path to get wrong.
- 1
Put
notes.txtin the same folder as your program and open it by name. Then move the file to the desktop and run the program again.Hint · The second run cannot find it. Nothing in the program changed.
- 2
Make a folder called
datainside your program's folder, put the file in it, and open it from there.Hint · open('data/notes.txt') — one step down.
- 3
Print
len()of"C:\new\table.txt"and ofr"C:\new\table.txt".Hint · 14 and 16. The two missing characters were your folders.
- 4
Write a program that opens a file that is not there and prints a message instead of stopping.
Hint · try, then except FileNotFoundError.
A program opens 'notes.txt'. Where does Python look for it?
What is len("C:\\new\\table.txt")?
Which of these does NOT work as a Windows path?
What does '../data/notes.txt' mean?