automatically rename to unique on dupe

This commit is contained in:
Victor Hall 2022-11-04 14:24:00 -04:00
parent 7ad2e6ef4d
commit fad322cba3
1 changed files with 4 additions and 3 deletions

View File

@ -60,7 +60,7 @@ def get_replace_list(opt):
def rename_files(opt):
find_list = get_replace_list(opt)
for file in glob.glob(opt.img_dir + "/*"):
for idx, file in glob.iglob(opt.img_dir + "/*"):
print(file)
if os.path.splitext(file)[1] in (".jpg", ".png", ".jpeg", ".gif", ".bmp", ".webp"):
@ -74,8 +74,9 @@ def rename_files(opt):
try:
print(f"Renaming {file} to {new_filename}")
if os.path.exists(new_filename):
print(f"filename already exists, cannot rename:{file} to {new_filename}")
else:
new_filename = new_filename + "_" + idx
print(f"filename already exists, appended '_n' to {new_filename}")
try:
os.rename(file, new_filename)
except Exception as e: