16.3. Pathlib Path

  • Path.absolute() - Get the absolute path

  • Path.resolve() - Get the absolute path

  • Path.relative_to(other) - Get the relative path to another path

  • Path.match(pattern) - Check if the path matches a pattern

  • Path.is_absolute() - Check if the path is absolute

  • Path.is_relative_to(other) - Check if the path is relative to another path

  • Joining paths with the / operator

  • Path.glob() - Find files matching a pattern

  • Path.rglob() - Recursively find files matching a pattern

16.3.1. SetUp

>>> from pathlib import Path

16.3.2. Glob

SetUp:

>>> mydir = Path('/tmp/mydir/')
>>> mydir.mkdir(exist_ok=True)
>>> (mydir / 'myfile1.md').touch()
>>> (mydir / 'myfile2.md').touch()
>>> mydir.glob('*.md')
<map object at 0x...>

16.3.3. RGlob

16.3.4. Glob and Rglob

SetUp:

>>> mydir = Path('/tmp/mydir/')
>>> mydir.mkdir(exist_ok=True)
>>> (mydir / 'myfile1.md').touch()
>>> (mydir / 'myfile2.md').touch()

Usage:

>>> mydir.glob('*.md')
<map object at 0x...>
>>>
>>> sorted(mydir.glob('*.md'))
[PosixPath('/tmp/mydir/myfile1.md'), PosixPath('/tmp/mydir/myfile2.md')]
>>> sorted(mydir.rglob('*.md'))
[PosixPath('/tmp/mydir/myfile1.md'), PosixPath('/tmp/mydir/myfile2.md')]

16.3.5. Join Paths

>>> basedir = Path('/tmp')
>>> mydir = basedir / 'mydir'
>>> myfile = mydir / 'myfile.txt'
>>>
>>> myfile
PosixPath('/tmp/mydir/myfile.txt')
>>> myfile = Path('/') / 'tmp' / 'mydir' / 'myfile.txt'
>>>
>>> myfile
PosixPath('/tmp/mydir/myfile.txt')
>>> myfile = Path('/')
>>> myfile /= 'tmp'
>>> myfile /= 'mydir'
>>> myfile /= 'myfile.txt'
>>>
>>> myfile
PosixPath('/tmp/mydir/myfile.txt')

16.3.6. Assignments

# %% About
# - Name: Pathlib Path File
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Create Path object for file `FILE`
# 2. Define variable `result: Path` with the solution
# 3. Use `pathlib` built-in module
# 4. Run doctests - all must succeed

# %% Polish
# 1. Utwórz obiekt Path dla pliku `FILE`
# 2. Zdefiniuj zmienną `result: Path` z rozwiązaniem
# 3. Use `pathlib` built-in module
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected

# %% Hints
# - `pathlib.Path()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 10), \
'Python has an is invalid version; expected: `3.10` or newer.'

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert isinstance(result, Path), \
'Variable `result` has invalid type; expected: `Path`.'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
from pathlib import Path

# %% Types
result: Path

# %% Data
FILE = '_temporary.txt'

# %% Result
result = ...

# %% About
# - Name: Pathlib Path Directory
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Create Path object for directory `DIRECTORY`
# 2. Define variable `result: Path` with the solution
# 3. Use `pathlib` built-in module
# 4. Run doctests - all must succeed

# %% Polish
# 1. Utwórz obiekt Path dla katalogu `DIRECTORY`
# 2. Zdefiniuj zmienną `result: Path` z rozwiązaniem
# 3. Use `pathlib` built-in module
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected

# %% Hints
# - `pathlib.Path()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 10), \
'Python has an is invalid version; expected: `3.10` or newer.'

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert isinstance(result, Path), \
'Variable `result` has invalid type; expected: `Path`.'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
from pathlib import Path

# %% Types
result: Path

# %% Data
DIRECTORY = '.'

# %% Result
result = ...