Filepaths in Python — A Beginner’s Guide
Do you want to write data into a file or read data from a file? Or check what files and folders are in a certain directory? Python is well suited for all the above.
In order to work with a file or directory you have to know its name. But not only the name is important, you also have to know where the file or directory is stored. That place is specified with the path.
There are 2 types of filepaths:
- Absolute filepaths describe the path from the root directory to the file.
- Relative filepaths describe the path from the Python script to the file.
In this article I will tell you what I know about working with both types of filepaths so that you can easily work with files and directories as well.

Example Files and Directories
To be able to explain how filepaths work, I will first show you some files and directories on my computer that will serve as examples.
On the C drive of my computer there is a Users directory. In the Users directory there is a directory for each user. For example there is a directory called BE (for user BE).
In the BE directory there is a Documents directory and in the Documents directory I have prepared 5 directories:
- Directory A
Absolute filepath:
C:\Users\BE\Documents\AContains: DirectoryBand file1.txt - Directory B
Absolute filepath:
C:\Users\BE\Documents\A\BContains: DirectoryCand file2.txt - Directory C
Absolute filepath:
C:\Users\BE\Documents\A\B\CContains: DirectoryDand file3.txtand Python scriptmain.py - Directory D
Absolute filepath:
C:\Users\BE\Documents\A\B\C\DContains: DirectoryEand file4.txt - Directory E
Absolute filepath:
C:\Users\BE\Documents\A\B\C\D\EContains: File5.txt
All code in this article will be written in main.py which is stored in directory C.
Absolute filepaths and backslash trouble
If you want to use absolute filepaths, like the ones above, that is perfectly fine.
But working with filepaths that have backslashes (\) can cause trouble in Python. That has to do with the fact that backslashes are also used with escape characters.
import os
filepath = 'C:\Users\BE\Documents\A\B\C'
print(os.listdir())The above code gives an error: SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2–3: truncated \UXXXXXXXX escape
Here are some solutions to avoid the problem:
- use forward slashes instead of backward slashes:
filepath = 'C:/Users/BE/Documents/A/B/C' - replace the backslashes with double backslashes
filepath = 'C:\\Users\\BE\\Documents\\A\\B\\C' - use a raw string by putting an
rbefore the stringfilepath = r'C:\Users\BE\Documents\A\B\C'
Adding filenames to filepaths
If you want to work with a file you can add the filename to the filepath with a slash in between:
filename = '3.txt'
filepath = 'C:\\Users\\BE\\Documents\\A\\B\\C\\' + filename
with open(filepath) as f:
print(f.read())or
filename = '3.txt'
filepath = 'C:/Users/BE/Documents/A/B/C/' + filename
with open(filepath) as f:
print(f.read())Relative filepaths and the working directory
When we work with relative filepaths it is important to understand that this is the path between the file and the location of the Python script.
The directory in which a script is placed is also known as working directory.
You can determine the current working directory in Python with the getcwd function form the os package.
import os
print(os.getcwd())The above code correctly prints: C:\Users\BE\Documents\A\B\C
By default, Python uses the current working directory to look for directories and files. Here are 2 examples of that:
import os
print(os.listdir())
with open('3.txt') as f:
print(f.read())This prints:
['3.txt', 'D', 'main.py']
This is the content of 3.txtWe didn’t specify a directory when calling the listdir function and so it showed us the contents of the current working directory, which is C.
And when opening the file we only specified the filename 3.txt. Since we didn’t specify the filepath it looked for the file in the current working directory.
Relative filepaths to directories higher up
When talking about directories A and B, we could say they are ‘higher’ then our working directory C. A relative filepath would have 2 dots .. to specify to look 1 directory higher up.
And so print(os.listdir('..')) prints the content of directory B: ['2.txt', 'C'].
To open 2.txt we can use the following relative filepath: '../2.txt'.
with open('../2.txt') as f:
print(f.read())The above code prints: This is the content of 2.txt
If we want a relative filepath to A we would need to look 2 directories higher up. In that case the code would look like this:
import os
print(os.listdir('../..'))
with open('../../1.txt') as f:
print(f.read())This prints: ['1.txt', 'B'] and This is the content of 1.txt
Relative filepaths to directories deeper down
Instead of looking at directories higher up, we will now look at directories deeper down.
For example directory D is within directory C. A relative filepath to directory D from our working directory C would simply be: D.
print(os.listdir('D'))
with open('D/4.txt') as f:
print(f.read())The above code prints: ['4.txt', 'E'] and This is the content of 4.txt
And the relative filepath to directory E would be: D/E.
print(os.listdir('D/E'))
with open('D/E/5.txt') as f:
print(f.read())The above code prints: ['5.txt'] and This is the content of 5.txt
Thank you for reading!
You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:.
You might also like:






