Last active
November 2, 2020 14:34
-
-
Save janosh/870a76ad7f6699a4f1fda7ad83379520 to your computer and use it in GitHub Desktop.
Batch rename files sequentially while handling possible XMP sidecar files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).