# Быстрый\_импорт\_файлов\_по\_маске\_в\_Python

#### 📌 **Шпаргалка: Быстрый импорт файлов по маске в Python (аналог `glob` для `data_files`)**

**1. Импорт через `glob` (стандартный способ)**

```python
from glob import glob
import os

data_files = [
    # Launch-файлы
    (os.path.join('share', 'pkg_name', 'launch'), glob('launch/*.launch.py')),
    
    # Конфиги (YAML, JSON и др.)
    (os.path.join('share', 'pkg_name', 'config'), glob('config/*.yaml')),
    
    # Произвольные файлы (например, модели, карты)
    (os.path.join('share', 'pkg_name', 'models'), glob('assets/models/*.stl')),
]
```

**2. Рекурсивный поиск (если файлы в подпапках)**

```python
from glob import glob

# Все .yaml-файлы, включая поддиректории
config_files = glob('config/**/*.yaml', recursive=True)
```

**3. Фильтрация списка (если нужно исключить файлы)**

```python
import os
from glob import glob

all_launch_files = glob('launch/*.launch.py')
filtered_files = [f for f in all_launch_files if 'test' not in os.path.basename(f)]
```

**4. Альтернатива: `pathlib` (более современный стиль)**

```python
from pathlib import Path

launch_files = list(Path("launch").glob("*.launch.py"))
config_files = list(Path("config").rglob("*.yaml"))  # рекурсивно
```

***

#### 🚀 **Пример для `setup.py` (как в ROS2)**

```python
setup(
    # ...
    data_files=[
        ('share/pkg_name/launch', glob('launch/*.launch.py')),
        ('share/pkg_name/config', glob('config/*.yaml')),
    ],
)
```

***

#### ⚠️ **Важно!**

* **Пути указываются относительно `setup.py`**.
* **Для рекурсивного поиска** используйте `**/*.расширение` + `recursive=True` (в `glob`) или `Path.rglob()`.
* **Фильтрация** — если нужно исключить тестовые/временные файлы.

***

#### 🔍 **Проверка**

После сборки (`colcon build`) смотрите:

```bash
ls install/pkg_name/share/pkg_name/  # должны быть launch/, config/ и т.д.
```


---

# Agent Instructions: 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://alice-and-alex-docs.gitbook.io/alice_and_alex_docs/ros2-development/bystryi_import_failov_po_maske_v_python.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.
