Spaces:
Running
Running
File size: 1,346 Bytes
9fd1204 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import pathlib
import shutil
from pathlib import Path
from typing import List, Union
from finetrainers.logging import get_logger
logger = get_logger()
def find_files(root: str, pattern: str, depth: int = 0) -> List[str]:
root_path = pathlib.Path(root)
result_files = []
def within_depth(path: pathlib.Path) -> bool:
return len(path.relative_to(root_path).parts) <= depth
if depth == 0:
result_files.extend([str(file) for file in root_path.glob(pattern)])
else:
for file in root_path.rglob(pattern):
if not file.is_file() or not within_depth(file.parent):
continue
result_files.append(str(file))
return result_files
def delete_files(dirs: Union[str, List[str], Path, List[Path]]) -> None:
if not isinstance(dirs, list):
dirs = [dirs]
dirs = [Path(d) if isinstance(d, str) else d for d in dirs]
logger.debug(f"Deleting files: {dirs}")
for dir in dirs:
if not dir.exists():
continue
shutil.rmtree(dir, ignore_errors=True)
def string_to_filename(s: str) -> str:
return (
s.replace(" ", "-")
.replace("/", "-")
.replace(":", "-")
.replace(".", "-")
.replace(",", "-")
.replace(";", "-")
.replace("!", "-")
.replace("?", "-")
)
|