Created
May 31, 2017 05:10
-
-
Save kevinpostal/fd52009432531edbc515ff0638a661d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import itertools | |
from os import listdir | |
from os.path import isfile, join | |
from collections import Counter | |
filelist = [] | |
def normalize_filename(filename): | |
return {"name": filename, "group": re.sub(r'\d+', '(\d+)', filename)} | |
mypath = "." | |
file_list = sorted( | |
[ | |
normalize_filename(f) for f in listdir(mypath) | |
if isfile(join(mypath, f)) | |
], | |
key=lambda x: x['name']) | |
groups = itertools.groupby(file_list, lambda x: x['group']) | |
for name, files in groups: | |
name = files.next() | |
fileNumbers = [ | |
re.match(file['group'], file['name']).groups() for file in list(files) | |
] | |
if fileNumbers: | |
filelist.append({ | |
"file": name.get("name"), | |
"group": name.get("group"), | |
"tuple": fileNumbers, | |
"tuple_count": Counter(item[0] for item in fileNumbers), | |
"count": len(fileNumbers) + 1 | |
}) | |
else: | |
filelist.append({"file": name.get("name"), "count": 1}) | |
for item in filelist: | |
if item.get("group"): | |
filename = item.get("group") | |
fileconcat = item.get("tuple") | |
for numbervalue, itemcount in item.get("tuple_count").items(): | |
import pdb; pdb.set_trace() | |
replace1 = re.sub(r'\(\\d.\)', fileconcat[0], filename, count=1) | |
filename = re.sub(r'\(\\d.\)', fileconcat[0][1], replace1, count=1) | |
else: | |
filename = item.get("file") | |
print "%d %s" % (item.get("count"), filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment