Skip to content

Instantly share code, notes, and snippets.

@janosh
Last active November 2, 2020 14:34
Show Gist options
  • Save janosh/870a76ad7f6699a4f1fda7ad83379520 to your computer and use it in GitHub Desktop.
Save janosh/870a76ad7f6699a4f1fda7ad83379520 to your computer and use it in GitHub Desktop.
Batch rename files sequentially while handling possible XMP sidecar files.
import os
import sys
_, dirname, prefix = sys.argv
os.chdir(dirname)
files = sorted(f for f in os.listdir() if not f.endswith(".xmp"))
for idx, file in enumerate(files, 1):
basename, ext = os.path.splitext(file)
os.rename(file, f"{prefix}{idx}{ext}")
if os.path.exists(f"{basename}.xmp"):
os.rename(f"{basename}.xmp", f"{prefix}{idx}.xmp")
@janosh
Copy link
Author

janosh commented Nov 2, 2020

A small Python script that handles the use case of renaming media files and assigning the same new name to the corresponding XMP file if one exists.

Requires Python v3.6 or later and expects two arguments passed via the command line, the first being the target directory of files to be renamed and the second the prefix to be inserted in front of the sequence counter. It also assumes the files are to be sequenced in alphabetical order (so best rename the files according to their creation date in your DAM first).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment