Cleaning some music collection converters.

This commit is contained in:
James Grogan 2024-11-10 11:50:33 +00:00
parent b39807db44
commit c09179f5da
8 changed files with 94 additions and 80 deletions

View file

@ -7,25 +7,29 @@ def get_files_recursive(path: Path, extensions: list[str] | None) -> list[Path]:
if not extensions:
return list(path.rglob("*.*"))
else:
ret = []
for ext in extensions:
ret.append(list(search_path.rglob(f"*.{ext}")))
return ret
paths_list = [list(path.rglob(f"*.{ext}")) for ext in extensions]
return [p for paths in paths_list for p in paths]
def get_empty_dirs(path: Path) -> list[Path]:
return [curdir
for curdir, subdirs, files in os.walk(path)
if len(subdirs)==0 and len(files) == 0]
def _delete_empty_dirs(path: Path) -> bool:
found_empty = False
for curdir, subdirs, files in os.walk(path):
if len(subdirs) == 0 and len(files) == 0:
found_empty = True
os.rmdir(curdir)
return found_empty
empty_dirs = get_empty_dirs(path)
map(os.rmdir, empty_dirs)
return bool(empty_dirs)
def delete_empty_dirs_recursive(path: Path):
found_empty = True
while found_empty:
found_empty = _delete_empty_dirs(path)
def replace_filename(path: Path, replacement: str) -> Path:
return path.parent / (replacement + path.suffix)
def move_file_new_name(src: Path, dst: Path, name: str):
os.makedirs(dst, exist_ok=True)
shutil.move(src, dst / f"{name}{src.suffix}")