Skip to content

Instantly share code, notes, and snippets.

@kevinpostal
Created May 31, 2017 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinpostal/fd52009432531edbc515ff0638a661d7 to your computer and use it in GitHub Desktop.
Save kevinpostal/fd52009432531edbc515ff0638a661d7 to your computer and use it in GitHub Desktop.
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