Skip to content

Instantly share code, notes, and snippets.

@xAlpharax
Created August 1, 2020 13:40
Show Gist options
  • Save xAlpharax/5da6c016b1cd4dc83dac87afd0f334da to your computer and use it in GitHub Desktop.
Save xAlpharax/5da6c016b1cd4dc83dac87afd0f334da to your computer and use it in GitHub Desktop.
Rename same filenames from entire directory
import os
import filecmp
dirnow = os.getcwd()
print("Now in: {} \n".format(dirnow))
names_seen = list()
def renamestuff(path):
n = 1
for root, dirs, files in os.walk(path):
for name in files:
filePath = os.path.join(os.path.abspath(root), name)
if n == 20:
n = 1
if name.lower() in names_seen:
newname = name.replace('.', ' ({}).'.format(n))
os.rename(filePath, os.path.join(os.path.abspath(root), newname))
n += 1
# names_seen.append(newname)
else:
names_seen.append(name)
n = 1
print(name)
return
renamestuff(os.path.join(dirnow, 'dir'))
print('\n', names_seen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment