11.2. Pathlib Dirs
11.2.1. SetUp
>>> from pathlib import Path
>>> from shutil import rmtree
>>>
>>> rmtree('/tmp/a', ignore_errors=True)
>>> Path('/tmp/myfile.txt').unlink(missing_ok=True)
11.2.2. Create Directories
>>> from pathlib import Path
>>>
>>>
>>> path = Path('/tmp/a')
>>> path.mkdir()
>>> from pathlib import Path
>>>
>>>
>>> path = Path('/tmp/a')
>>> path.mkdir()
Traceback (most recent call last):
FileExistsError: [Errno 17] File exists: '/tmp/a'
>>> from pathlib import Path
>>>
>>>
>>> path = Path('/tmp/a')
>>> path.mkdir(exist_ok=True)
11.2.3. Create Directory Hierarchy
>>> from pathlib import Path
>>>
>>>
>>> path = Path('/tmp/a/b/c')
>>> path.mkdir(parents=True, exist_ok=True)
11.2.4. Delete directory
Works only with empty directories:
>>> from pathlib import Path
>>>
>>>
>>> path = Path('/tmp/a')
>>> path.rmdir()
Traceback (most recent call last):
OSError: [Errno ...] Directory not empty: '/tmp/a'
Remove directories with files:
>>> from shutil import rmtree
>>>
>>>
>>> path = '/tmp/a'
>>> rmtree(path, ignore_errors=True)
11.2.5. Create File
Touch creates a file
>>> from pathlib import Path
>>>
>>>
>>> file = Path('/tmp/myfile.txt')
>>> file.touch()
11.2.6. Check If Exists
>>> from pathlib import Path
>>>
>>>
>>> file = Path('/tmp/myfile.txt')
>>> file.exists()
True
11.2.7. Is File or Dir
>>> from pathlib import Path
>>>
>>>
>>> file = Path('/tmp/myfile.txt')
>>>
>>> file.is_dir()
False
>>>
>>> file.is_file()
True
11.2.8. Assignments
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: File Path Exception
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define variables:
# - `result_a: bool` with result of checking if path `a` exists
# - `result_b: bool` with result of checking if path `b` exists
# 2. Use `Path` object from `pathlib`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienne:
# - `result_a: bool` z wynikiem sprawdzenia czy ścieżka `a` istnieje
# - `result_b: bool` z wynikiem sprawdzenia czy ścieżka `b` istnieje
# 2. Użyj `Path` object z `pathlib`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_a is not Ellipsis, \
'Assign your result to function `result_a`'
>>> assert result_b is not Ellipsis, \
'Assign your result to function `result_b`'
>>> result_a
True
>>> result_b
False
"""
from pathlib import Path
a = Path.cwd()
b = Path.cwd() / 'notexisting.txt'
result_a = ...
result_b = ...
# FIXME: exists()
# FIXME: is_file()
# FIXME: is_dir()
# FIXME: is_symlink()
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: File Path Abspath
# - Difficulty: easy
# - Lines: 3
# - Minutes: 5
# %% English
# 1. Define `path` with converted `filename` to absolute path
# 2. To `result` assgin string:
# - `file` if path is a file
# - `directory` if path is a directory
# - `missing` if path does not exist
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `path` z przekonwertowym `filename` do ścieżki bezwzględnej
# 2. Do `result` przypisz ciąg znaków:
# - `file` jeżeli ścieżka jest plikiem
# - `directory` jeżeli ścieżka jest katalogiem
# - `missing` jeżeli ścieżka nie istnieje
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `from pathlib import Path`
# - `Path.cwd()`
# - `Path()`
# - `Path.is_dir()`
# - `Path.is_file()`
# - `Path.exists()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert isinstance(result, str), \
'Result must be a str with: `file`, `directory` or `missing`'
>>> assert isinstance(abspath, Path), \
'Use Path class from pathlib library to create a filepath'
>>> current_directory = Path.cwd()
>>> assert str(current_directory) in str(abspath), \
'File Path must be absolute, check if you have current directory in path'
>>> result
'missing'
"""
from pathlib import Path
FILENAME = 'myfile.txt'
# Absolute path to FILENAME
# type: Path
abspath = ...
# File, directory or missing
# type: str
result = ...