Finding duplicate file names and modifying them based on elements of the path

MRAB python at
Thu Jul 19 23:32:46 CEST 2012

On 19/07/2012 20:06, Larry.Martell at wrote:
> On Jul 19, 1:02 pm, "Prasad, Ramit" <ramit.pra... at> wrote:
>> > > I am making the assumption that you intend to collapse the directory
>> > > tree and store each file in the same directory, otherwise I can't think
>> > > of why you need to do this.
>> > Hi Simon, thanks for the reply. It's not quite this - what I am doing
>> > is creating a zip file with relative path names, and if there are
>> > duplicate files the parts of the path that are not be carried over
>> > need to get prepended to the file names to make then unique,
>> Depending on the file system of the client, you can hit file name
>> length limits. I would think it would be better to just create
>> the full structure in the zip.
>> Just something to keep in mind, especially if you see funky behavior.
> Thanks, but it's not what the client wants.
Here's another solution, not using itertools:

from collections import defaultdict
from os.path import basename, dirname
from time import strftime, strptime

# Starting with the original paths

paths = [

def make_dir5_key(path):
     date = strptime(path.split("/")[6], "%d%b%y")
     return strftime("%y%b%d", date)

# Collect the paths into a dict keyed by the basename

files = defaultdict(list)
for path in paths:

# Process a list of paths if there's more than one entry

renaming = []

for name, entries in files.items():
     if len(entries) > 1:
         # Collect the paths in each subgroup into a dict keyed by dir4

         subgroup = defaultdict(list)
         for path in entries:

         for dir4, subentries in subgroup.items():
             # Sort the subentries by dir5 (date)

             if len(subentries) > 1:
                 for index, path in enumerate(subentries):
"{}/{}_{:02}_{}".format(dirname(path), dir4, index, name)))
                 path = subentries[0]
                 renaming.append((path, "{}/{}_{}".format(dirname(path), 
dir4, name)))
         path = entries[0]

for old_path, new_path in renaming:
     print("Rename {!r} to {!r}".format(old_path, new_path))

More information about the Python-list mailing list