PYTHON

Python And File Paths

Date Published:
Last Modified:

Overview

The pathlib Module

The pathlib module was introduced in Python v3.4 (PEP 428). It’s purpose was to replace the much used functions such as os.path.join() and family with a simpler set of path manipulating classes/functions which work similarly across all platforms. In the words of PEP 428, it is “object-orientated file-system paths”.

On Linux/macOS:

file_path = Path('~/my_file.txt')

On Windows you would also use forward slashes to describe paths! The Path module recognizes you are running the code on Windows and converts the path accordingly.

file_path = Path('C:/Users/my_file.txt')

os.path.join() has long been the mainstay of concatenating paths in cross-platform way. For instance, os.path.join('my_dir', 'my_file') would result in the string my_dir/my_file in Linux-like (POSIX) systems, and my_dir\my_file on Windows systems. The pathlib module aims to make os.path.join() redundant by overloading the / (slash) operator to allow the concatenation of path segments:

file_path = Path('my_dir') / 'my_file'

Once you have a Path object, you can check if something exists at that path (typically it would point to a directory or file) by calling .exists():

my_path = Path('my_file.txt')
my_path.exists()
# Returns True if text file exists

Backwards Compatibility

If you start using Path objects in your code but have to interacts with pre-existing code which uses plain strings for paths, you will have to convert the Path object to a string first, which is easily done with the str() cast:

my_path = Path('my_file.txt')
old_function(str(my_path))

Like this page? Upvote with shurikens!

Related Content:

Tags:

comments powered by Disqus