LambdaLabTM
Computer Science · Class 12 · Text Files
Text filesPaths⏱️ 12 min read

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.

This lesson has a first half
Relative & Absolute Paths in the File Handling chapter explains paths on their own. This lesson is about giving one to open().

1Two kinds of address

The string you write inside open() is one of two things.

Relative — short directions
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.

Absolute — the full address
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.

Key Takeaway
Almost every school program uses the first kind. Keep the data file in the same folder as your program and open it by its name. That is short to write, it is what the board expects, and it still works when you hand the folder in on a pen drive.

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.txt

The program is in programs. So 'marks.csv' is found and 'notes.txt' is not — it exists, but it is in the other folder:

finding.py
# 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()
Output
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.

🧭 Which file does this address reach?

The program is finding.py, inside programs. Tap an address.

the folders
school
programs
finding.pythe program
marks.csvfound
data
notes.txt
f = open('marks.csv', 'r')
what happens
opened
the walk
programs

Just a name, so Python looks in the folder the program is running from — programs. marks.csv is right there.

“File not found” usually means “wrong folder”
The file is nearly always there. What is wrong is the address. Before you go looking for the file, look at the folder your program is sitting in.

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

You already do this every day. Open a folder in File Explorer, then press back — you are one folder out again. .. 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:

broken_path.py
bad = "C:\new\table.txt"
print(bad)
print(len(bad))
Output
C:
ew	able.txt
14
No error, and the wrong path
\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.

Way 1
Write each backslash twice
"C:\\school\\notes.txt"

Two backslashes in a string mean one real backslash. It works, but it is easy to miscount them.

Way 2
Put an r before the quote
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.

Way 3
Use forward slashes
"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:

three_ways.py
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)
Output
C:\school\data\notes.txt
C:\school\data\notes.txt
C:/school/data/notes.txt
True
Which one should you write?
The forward slash. All three are right, but this one cannot be miscounted, and the same line also runs on a Mac or a Linux computer. Use the raw string when you have copied a path out of a Windows folder window and do not want to edit it.
A raw string cannot end with a backslash
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

1
Is the file in the same folder as the program?

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.

2
Is the name spelt exactly right?

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.

3
Are there stray backslashes in the string?

Print the path on its own line before opening it. If what comes out is not what you typed, that is the bug.

4
Is the mode the one you meant?

'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:

polite.py
try:
    f = open('notes.txt', 'r')
    print(f.read())
    f.close()
except FileNotFoundError:
    print('notes.txt is not in this folder.')
Output
notes.txt is not in this folder.

6Which kind should you write?

Let's Recap!
Relative pathAbsolute path
Looks like'notes.txt', 'data/notes.txt''C:/school/notes.txt'
Starts fromthe folder the program is inthe drive
Lengthshortlong
Copy the folder to another computerstill worksbreaks
Use it forthe data file that goes with your programa file that always lives in one fixed place

7Recap

The first thing in open() is an address

Not just a name. Python looks exactly where it says, and nowhere else.

A plain name means 'my own folder'

The folder the program is running from. A file anywhere else needs a longer address.

A name after a slash goes down, .. goes back

.. is the back button of File Explorer, written down. '../data/notes.txt' goes back one folder and then down into data.

A backslash in a string is not a backslash

"C:\new\table.txt" is 14 characters and points nowhere.

Three safe ways to write a Windows path

Double each backslash, put an r before the quote, or use forward slashes. The forward slash is the easiest.

'File not found' means the address is wrong

Wrong folder, wrong spelling, or a backslash Python read as something else.

And in the exam, it is nearly always just the file name
Everything on this page — .., 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:
the_usual_answer.py
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.

✍️ Now write these yourself
  1. 1

    Put notes.txt in 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. 2

    Make a folder called data inside your program's folder, put the file in it, and open it from there.

    Hint · open('data/notes.txt') — one step down.

  3. 3

    Print len() of "C:\new\table.txt" and of r"C:\new\table.txt".

    Hint · 14 and 16. The two missing characters were your folders.

  4. 4

    Write a program that opens a file that is not there and prints a message instead of stopping.

    Hint · try, then except FileNotFoundError.

Quick Check

A program opens 'notes.txt'. Where does Python look for it?

Quick Check

What is len("C:\\new\\table.txt")?

Quick Check

Which of these does NOT work as a Windows path?

Quick Check

What does '../data/notes.txt' mean?