I have a directory with some image sequences that follow a similar naming convention, for example: test.1001.exr, test.1002.exr, test.1001.dpx, test.1002.dpx.
I'm trying to write some code that would output the following:
test.1001-1002.exr
test.1001-1002.exr
I found an example which does list them in the way I would like, however it is not very robust and would not show the file extension unless I print the specific one.
scans = []
for root, dirnames, filenames in os.walk(scanDir):
for file in filenames:
for ext in fileFormats:
if ext in file:
scans.append(file)
scans = [os.path.splitext(file)[0] for file in scans]
scans.sort()
def extract_number(name):
return re.findall(r"\d+$", name)[0]
def collapse_group(group):
if len(group) == 1:
return group[0][1] # Unique names collapse to themselves.
first = extract_number(group[0][1]) # Fetch range
last = extract_number(group[-1][1]) # of this group.
length = len(str(int(last)))
return "%s%s-%s%s" % (group[0][1][:-length],
first[-length:], last[-length:], ".dpx")
groups = [collapse_group(tuple(group)) \
for key, group in itertools.groupby(enumerate(scans),
lambda index_name: index_name[0] - int(extract_number(index_name[1])))]
else:
groups = []
I'm not sure I'm able to modify the code to achieve my result. Does anyone know a way I can do this?
extract_number
for you can be as simple asname.split('.')[1]
. No regex necessary. - Tim Roberts