Skip to content

Latest commit

 

History

History
100 lines (71 loc) · 2.01 KB

pathlib.md

File metadata and controls

100 lines (71 loc) · 2.01 KB

Working with paths in Python using pathlib

pathlib is a Python Standard Library module created to make it easier to work with paths in a file system. This module debuted in Python 3.4 and updated in Python 3.5.

The anatomy of a pathlib.Path on Windows

from pathlib import Path

path = Path(r'C:/Users/Me/projects/blog/config.tar.gz')

path.drive
# 'C:'

path.root
# '/'

path.root
# 'C:/'

path.parent
# WindowsPath('C:/Users/Me/projects/blog')

path.name
# 'config.tar.gz'

path.stem
# 'config.tar'

path.suffix
# '.gz'

path.suffixes
# ['.tar', '.gz']

Working with paths

Creating paths and directories

When we convert a WindowsPath to string, Python adds backslashes. repr returns the path with forward slashses as it is represented on Windows.

path = Path(r'C:/Users/Me/projects/blog/config.tar.gz')

str(path)
# 'C:\\Users\\Me\\projects\\blog\\config.tar.gz'

repr(path)
# "WindowsPath('C:/Users/Me/projects/blog/config.tar.gz')"

Creating and joining paths:

Path('.', 'projects', 'python', 'source')
# WindowsPath('projects/python/source')

Path('.', 'projects', 'python') / Path('source')
# WindowsPath('projects/python/source')

Path('.', 'projects', 'python') / 'source'
# WindowsPath('projects/python/source')

Creating directories:

path = Path('new_directory')
path.mkdir()

path.exists()
# True

When you have a directory path and it already exists, Python raises FileExistsError if you call Path.mkdir() on it.

Create parent directories recursively if not exists:

path = Path('new_parent_dir/sub_dir')
path.mkdir(parents=True)

List all files and directories

List all files and and directories:

path = Path('/home/Me/projects/pathlib')
list(path.iterdir())

List only directories:

[p for p in path.iterdir() if p.is_dir()]

List only files:

[p for p in path.iterdir() if p.is_file()]

References