13.1. File Path
Relative paths are relative to the current working directory
Absolute paths start with root directory
Python works with both relative and absolute path
13.1.1. Relative Paths
Path is relative to currently running script
Relative paths works the same on Windows and
*nix
(Linux, macOS, BSD, etc.).
- Current directory..
- Parent directory
Current directory:
>>> file = 'myfile.csv'
>>> file = 'data/myfile.csv'
>>> file = 'data/csv/myfile.csv'
>>> file = './myfile.csv'
>>> file = './data/myfile.csv'
>>> file = './data/csv/myfile.csv'
Parent directory:
>>> file = '../myfile.csv'
>>> file = '../data/myfile.csv'
>>> file = '../data/csv/myfile.csv'
>>> file = '../../myfile.csv'
>>> file = '../../data/myfile.csv'
>>> file = '../../data/csv/myfile.csv'
>>> file = '../../../myfile.csv'
>>> file = '../../../data/myfile.csv'
>>> file = '../../../data/csv/myfile.csv'
13.1.2. Absolute Paths
Absolute path include all entries in the directories hierarchy
Absolute path on
*nix
starts with root/
dirAbsolute path on Windows starts with drive letter
Linux (and other *nix):
>>> file = '/home/mwatney/myfile.csv'
macOS:
>>> file = '/Users/mwatney/myfile.csv'
Windows:
>>> file = 'c:/Users/mwatney/myfile.csv'
13.1.3. Windows Path Problem
Problem with paths on Windows
Use backslash (
\\
) as a path separatorUse r-string for paths
Let's say we have a path to a file:
>>> print('C:/Users/mwatney/newfile.txt')
C:/Users/mwatney/newfile.txt
Paths on Windows do not use slashes (/
). You must use backslash (\\
)
as a path separator. This is where all problems starts. Let's start changing
slashes to backslashes from the end (the one before newfile.txt
):
>>> print('C:/Users/mwatney\newfile.txt')
C:/Users/mwatney
ewfile.txt
This is because \n
is a newline character. In order this to work
we need to escape it.
Now lets convert another slash to backslash, this time the one before
directory named mwatney
:
>>> print('C:/Users\mwatney\newfile.txt')
SyntaxWarning: invalid escape sequence '\m'
C:/Users/mwatney
ewfile.txt
Since Python 3.12 all non-existing escape characters (in this case \m
will need to be escaped or put inside of a row strings. This is only a
warning (SyntaxWarning: invalid escape sequence '\m'
, so we can ignore
it, but this behavior will be default sometime in the future, so it is better
to avoid it now.
The last slash (the one before Users
):
>>> print('C:\Users\\mwatney\\newfile.txt')
Traceback (most recent call last):
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
This time the problem is more serious. Problem is with \Users
. After
escape sequence \U
Python expects hexadecimal Unicode codepoint, i.e.
\U0001F600
which is a smiley 😀 emoticon emoticon. In this example,
Python finds letter s
, which is invalid hexadecimal character and
therefore raises an SyntaxError
telling user that there is an error
with decoding bytes. The only valid hexadecimal numbers are
0123456789abcdefABCDEF
and letter s
isn't one of them.
There is two ways how you can avoid this problem. Using escape before every slash:
>>> print('C:\\Users\\mwatney\\newfile.txt')
C:\Users\mwatney\newfile.txt
Or use r-string:
>>> print(r'C:\Users\mwatney\newfile.txt')
C:\Users\mwatney\newfile.txt
Both will generate the same output, so you can choose either one. In my opinion r-strings are less error prone and I use them each time when I have to deal with paths.
13.1.4. Convention
Never hardcode paths, use constant as a file name or file path
Convention (singular form):
FILE
,FILENAME
,FILEPATH
,PATH
Convention (plural form):
FILES
,FILENAMES
,FILEPATHS
,PATHS
Note, that
PATH
is usually used for other purposes (sys.path
oros.getenv('PATH')
)
>>> FILE = 'myfile.txt'
>>> FILES = [
... 'myfile1.txt',
... 'myfile2.txt',
... 'myfile3.txt',
... ]
13.1.5. Recap
Relative paths are relative to the current working directory
Absolute paths start with root directory
Python works with both relative and absolute path
Path separator
/
(slash) is used on*nix
operating systems: Linux, macOS, BSD and other POSIX compliant OSes (excluding older versions of Windows)Path separator
\
(backslash) is used on WindowsIn newer Windows versions both
\
and/
works the same.
- Current directory..
- Parent directoryAbsolute path on
*nix
starts with root/
dirAbsolute path on Windows starts with drive letter
Never hardcode paths, use constant as a file name or file path
On Windows use r-string for paths