Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pathlib
- import pprint
- import shutil
- def unflacify(path):
- if path.parts[-2] == "flac":
- return path.parent.parent / path.relative_to(path.parent)
- return path
- def main(path="."):
- root = pathlib.Path(path).absolute()
- flac_root = root / "flac"
- new_paths = {
- old_path.absolute(): unflacify(
- flac_root / old_path.absolute().relative_to(root)
- )
- for old_path in list(pathlib.Path(path).glob("**/*.flac"))
- }
- for (old_path, new_path) in new_paths.items():
- new_path.parent.mkdir(parents=True, exist_ok=True)
- print(f"Moving {old_path} to {new_path}")
- old_path.rename(new_path)
- for empty_flac_dir in pathlib.Path(root).glob("**/flac"):
- if not empty_flac_dir.samefile(flac_root):
- try:
- empty_flac_dir.rmdir()
- except OSError:
- remaining_files = [
- path
- for path in empty_flac_dir.glob("**/*")
- if ".png" not in path.suffixes
- and ".jpg" not in path.suffixes
- and ".PNG" not in path.suffixes
- and ".JPG" not in path.suffixes
- ]
- if not remaining_files:
- shutil.rmtree(str(empty_flac_dir))
- else:
- remaining_files_str = ", ".join(map(str, remaining_files))
- raise RuntimeError(
- f"Not deleting {empty_flac_dir}, remaining files: {remaining_files_str}"
- )
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement