0
# File I/O
1
2
File system utilities for finding and organizing files with pattern matching and filtering capabilities.
3
4
## Capabilities
5
6
### File Finding
7
8
Utilities for recursively finding files based on various criteria.
9
10
```python { .api }
11
def find_files(substring, path, recursive=True, check_ext=None, ignore_invisible=True):
12
"""
13
Find files matching specified criteria.
14
15
Parameters:
16
- substring: str, substring that must be in filename
17
- path: str, directory path to search in
18
- recursive: bool, search subdirectories recursively
19
- check_ext: str, file extension to filter by (e.g., '.txt')
20
- ignore_invisible: bool, ignore hidden files (starting with '.')
21
22
Returns:
23
- matching_files: list, list of file paths matching criteria
24
"""
25
26
def find_filegroups(paths, substring='', extensions=None, ignore_invisible=True):
27
"""
28
Group files by specified criteria from multiple paths.
29
30
Parameters:
31
- paths: list, list of directory paths to search
32
- substring: str, substring filter for filenames
33
- extensions: list, list of file extensions to include
34
- ignore_invisible: bool, ignore hidden files
35
36
Returns:
37
- file_groups: dict, dictionary grouping files by criteria
38
"""
39
```
40
41
## Usage Examples
42
43
```python
44
from mlxtend.file_io import find_files, find_filegroups
45
import os
46
47
# Find all Python files in current directory
48
python_files = find_files(
49
substring='',
50
path='.',
51
recursive=True,
52
check_ext='.py',
53
ignore_invisible=True
54
)
55
print(f"Found {len(python_files)} Python files")
56
57
# Find files with specific substring
58
config_files = find_files(
59
substring='config',
60
path='.',
61
recursive=True,
62
ignore_invisible=True
63
)
64
print(f"Found {len(config_files)} files containing 'config'")
65
66
# Group files by extensions
67
file_groups = find_filegroups(
68
paths=['.', './docs'],
69
extensions=['.py', '.md', '.txt'],
70
ignore_invisible=True
71
)
72
print("File groups by extension:")
73
for ext, files in file_groups.items():
74
print(f" {ext}: {len(files)} files")
75
```