16.2. Pathlib Current — Python
16.2.1. Current Working Directory
Get current working directory with
Path.cwd()
>>> from pathlib import Path >>> >>> result = Path.cwd() >>> print(result) /home/myuser/myproject
16.2.2. Running File
Get path of the running script
>>> from pathlib import Path >>> >>> result = Path(__file__) >>> print(result) /home/myuser/myproject/myfile.py
16.2.3. Running Directory
Get directory of the running script
Base directory
>>> from pathlib import Path >>> >>> result = Path(__file__).parent >>> print(result) /home/myuser/myproject
16.2.4. Assignments
# %% About # - Name: Pathlib Current CWD # - 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 current working 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 bieżącego katalogu roboczego # 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()` # - `Path.cwd()` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 10), \ 'Python has an is invalid version; expected: `3.10` or newer.' >>> cwd = Path.cwd() >>> 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`.' >>> assert result == cwd, \ 'Variable `result` has invalid value; expected current working directory 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 # %% Result result = ...
# %% About # - Name: Pathlib Current 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 with the running script # 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 z uruchamianym skryptem # 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 # %% Result result = ...
# %% About # - Name: Pathlib Current 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 with the running script # 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 z uruchamianym skryptem # 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 # %% Result result = ...