Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. import pathlib
  2. import pprint
  3. import shutil
  4.  
  5.  
  6. def unflacify(path):
  7. if path.parts[-2] == "flac":
  8. return path.parent.parent / path.relative_to(path.parent)
  9. return path
  10.  
  11.  
  12. def main(path="."):
  13. root = pathlib.Path(path).absolute()
  14. flac_root = root / "flac"
  15. new_paths = {
  16. old_path.absolute(): unflacify(
  17. flac_root / old_path.absolute().relative_to(root)
  18. )
  19. for old_path in list(pathlib.Path(path).glob("**/*.flac"))
  20. }
  21.  
  22. for (old_path, new_path) in new_paths.items():
  23. new_path.parent.mkdir(parents=True, exist_ok=True)
  24. print(f"Moving {old_path} to {new_path}")
  25. old_path.rename(new_path)
  26.  
  27. for empty_flac_dir in pathlib.Path(root).glob("**/flac"):
  28. if not empty_flac_dir.samefile(flac_root):
  29. try:
  30. empty_flac_dir.rmdir()
  31. except OSError:
  32. remaining_files = [
  33. path
  34. for path in empty_flac_dir.glob("**/*")
  35. if ".png" not in path.suffixes
  36. and ".jpg" not in path.suffixes
  37. and ".PNG" not in path.suffixes
  38. and ".JPG" not in path.suffixes
  39. ]
  40. if not remaining_files:
  41. shutil.rmtree(str(empty_flac_dir))
  42. else:
  43. remaining_files_str = ", ".join(map(str, remaining_files))
  44. raise RuntimeError(
  45. f"Not deleting {empty_flac_dir}, remaining files: {remaining_files_str}"
  46. )
  47.  
  48.  
  49. if __name__ == "__main__":
  50. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement