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 = [
"/dir0/dir1/dir2/dir3/qwer/09Jan12/dir6/file3",
"/dir0/dir1/dir2/dir3/abcd/08Jan12/dir6/file1",
"/dir0/dir1/dir2/dir3/abcd/08Jan12/dir6/file2",
"/dir0/dir1/dir2/dir3/xyz/08Jan12/dir6/file1",
"/dir0/dir1/dir2/dir3/qwer/07Jan12/dir6/file3",
]
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:
files[basename(path)].append(path)
# 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:
subgroup[path.split("/")[5]].append(path)
for dir4, subentries in subgroup.items():
# Sort the subentries by dir5 (date)
subentries.sort(key=make_dir5_key)
if len(subentries) > 1:
for index, path in enumerate(subentries):
renaming.append((path,
"{}/{}_{:02}_{}".format(dirname(path), dir4, index, name)))
else:
path = subentries[0]
renaming.append((path, "{}/{}_{}".format(dirname(path),
dir4, name)))
else:
path = entries[0]
for old_path, new_path in renaming:
print("Rename {!r} to {!r}".format(old_path, new_path))