> For the complete documentation index, see [llms.txt](https://olexsyn.gitbook.io/enote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://olexsyn.gitbook.io/enote/progr/python/modules/pathlib.md).

# pathlib

* <https://docs.python.org/3/library/pathlib.html> \[en]
* <https://docs.python.org/uk/3/library/pathlib.html> \[uk]
* [Использование модуля pathlib для манипуляции путями файловых систем в Python](https://www.digitalocean.com/community/tutorials/how-to-use-the-pathlib-module-to-manipulate-filesystem-paths-in-python-3-ru) \[digitalocean]
* [Почему вам следует использовать pathlib](https://habr.com/ru/post/453862/) \[habr]

```python
#!/usr/bin/python3

import sys
from pathlib import Path, PurePath


FULL_SCRIPT_PATH = PurePath(__file__)  # __file__ == sys.argv[0]
print('__file__ =', FULL_SCRIPT_PATH)

print(FULL_SCRIPT_PATH.name)
print(FULL_SCRIPT_PATH.stem)
print(FULL_SCRIPT_PATH.suffix)

SCRIPT_DIR = PurePath(__file__).parent
print('__file__.parent =', SCRIPT_DIR)

home = Path.home()
print('Path.home() =', home)

work = Path.cwd()
print('Path.cwd() =', work)

CONF_FILE = SCRIPT_DIR.parent / '_config' / 'config.ini'
print(f'{CONF_FILE=}')
```

```
// Run

$ /work/www/tests/config.py

// Output

sys.argv[0] = /work/www/domwatcher/tests/config.py
config.py
config
.py
sys.argv[0].parent = /work/www/domwatcher/tests
Path.home() = /home/olex
Path.cwd() = /home/olex
CONF_FILE=PurePosixPath('/work/www/domwatcher/_config/config.ini')

```

```python
from os      import replace, fspath
from pathlib import Path

XLS_DIR = Path(_conf.get('unv_dir'), 'cert_in')
# sheet = _xl.open( Path(XLS_DIR) / f'{equip_serial_number}.xls')

pattern = "*.xls"
xls_files = []
for currentFile in XLS_DIR.glob(pattern):
	xls_files.append(currentFile)

count_all = len(xls_files)
if count_all == 0:
	print(f"Нема жодного XLS-файлу у {XLS_DIR}")
elif count_all == 1:
	print(f'Знайдено файл {xls_files[0]}')
	cnt4job = int(input(f'Почати обробку? (0/1) '))
elif count_all > 1:
	print(f'Знайдено {count_all} файлів')
	cnt4job = int(input(f'Скільки файлів обробити? (0-{count_all}) '))
else:
	cnt4job = 0
	print('Щось пішло не так...')

print()
i = 0
for xls_file in xls_files:
	if i < cnt4job:
		sheet = _xl.open(xls_file)
		create_pdf(sheet)
		# xls_file - це Path, а '.ready' - рядок. os.fspath - перетворює Path в рядок
		replace(xls_file, Path( fspath(xls_file) + '.ready') )
		i += 1

print('Done')
```

Модуль [`glob`](https://docs.python.org/3/library/glob.html#module-glob) знаходить усі імена шляхів, що відповідають заданому шаблону, згідно з правилами, що використовуються оболонкою Unix, хоча результати повертаються в довільному порядку. Розгортання тильди не виконується, але `*`, `?`, і діапазони символів, виражені за допомогою `[]` , будуть правильно зіставлені. Це робиться шляхом спільного використання функцій [`os.scandir()`](https://docs.python.org/3/library/os.html#os.scandir) і [`fnmatch.fnmatch()`](https://docs.python.org/3/library/fnmatch.html#fnmatch.fnmatch) а не фактичного виклику підоболонки.

```python
LOGO_METEO = Path(_conf.get('img_dir')) / "logos/uhmc_logo_for_pdf.png"
EQUIP_PICT = Path(_conf.get('img_dir')) / "eq/101.jpg"
SIGNATURE  = Path(_conf.get('unv_dir')) / "signatures" / "10005.png"
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://olexsyn.gitbook.io/enote/progr/python/modules/pathlib.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
