We probably learn to deal with files and file paths pretty early on in our Python journey. We might use a string folder/subfolder/subsubfolder to represent a certain filepath.
I still use normal Python strings to build these filepaths until recently. But, working with large enterprise Python applications for a while has made me notice the error of my ways.
Why not use strings
- MacOS and Linux uses forward slashes in filepaths
/ - Windows uses backward slashes
\ - We probably use Windows on our work computers (some exceptions of course)
# this might break things in MacOS/Linux path = r'folder\subfolder\subsubfolder' # this might break things in Windows path = r'folder/subfolder/subsubfolder'
Do we really want to write if-else statements to build the file path every time? When we have a built-in solution that works perfectly.
Introducing pathlib
Note — this is a built-in module, so we don’t have to pip install it.
Let’s say we want to build the path folder/subfolder/subsubfolder in our Python script, and we want this to work seamlessly for Windows/MacOS/Linux/whatever environments.
# running this on a MacOS machine
import pathlib
path = pathlib.Path('folder') / 'subfolder' / 'subsubfolder'
print(path)
# folder/subfolder/subsubfolder
The pathlib.Path object creates a filepath that works with whatever system you are on.
- running this Python script on a MacOS/Linux machine will be the same as using the filepath
folder/subfolder/subsubfolder - running this same script on a Windows machine will be the same as using
folder\subfolder\subsubfolder
No more writing if-else statements to check whatever platform your machine is on just for the filepaths!