Back to blog
Feb 2024·1 min readAI-translated

Python3 - Renaming Files With the Same Name

#python

If you want to quickly and conveniently move/copy your files from a case-sensitive file system to a file system that is not case-sensitive, you can use this script to quickly rename files with duplicate names (a prefix is added).

As always - no guarantee or warranty for the script! Try it out first!

{{< highlight python "linenos=table,linenostart=1" >}} #!/usr/bin/python

import os import os.path

def walk(dir): files = [] directories = []

for path in os.listdir(dir): if os.path.isfile(os.path.join(dir, path)): files.append(path) if os.path.isdir(os.path.join(dir, path)): directories.append(path)

filesLower = [x.lower() for x in files]

for i in range(len(files)): fname = files[i]

if filesLower.count(fname.lower()) > 1: print("Rename " + dir + fname + " to " + dir + "_" + fname) os.rename(dir + fname, dir + "_"+ fname) files[i] = "_" + fname filesLower = [x.lower() for x in files]

for i in range(len(directories)): walk(dir + directories[i] + "/")

if __name__ == "__main__": walk("/path/to/root/folder") {{< / highlight >}}

NewerIdempotency Keys in REST APIsOlderMail Management / Spam Protection