Skip to content

Instantly share code, notes, and snippets.

@jvkersch
Created August 19, 2022 05:26
Show Gist options
  • Save jvkersch/95bd9a03eab05c18f9c0aeb1d8f7dcf0 to your computer and use it in GitHub Desktop.
Save jvkersch/95bd9a03eab05c18f9c0aeb1d8f7dcf0 to your computer and use it in GitHub Desktop.
import os
import shutil
def parse_dirname(dirname):
_, raw_name, _ = dirname.split(" - ")
parts = raw_name.split()
parts = [parts[-1]] + parts[:-1]
return " ".join(parts)
def rename_one(dirname, new_dirname):
# print(f"mv {dirname!r} {new_dirname!r}")
shutil.move(dirname, new_dirname)
def rename_all(directory):
for entry in os.listdir(directory):
try:
new_dirname = parse_dirname(entry)
except Exception:
continue
rename_one(entry, new_dirname)
if __name__ == "__main__":
rename_all(".")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment