Skip to content

Instantly share code, notes, and snippets.

@AyumuKasuga
Created September 20, 2013 12:49
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 AyumuKasuga/6636974 to your computer and use it in GitHub Desktop.
Save AyumuKasuga/6636974 to your computer and use it in GitHub Desktop.
import os
from collections import defaultdict
LIMIT = 1
file_dict = defaultdict(list)
remove_files = []
for filename in os.listdir('.'):
start = filename.split('-')[0]
file_dict[start].append(filename)
for group, filelist in file_dict.iteritems():
remove_files.extend(sorted(filelist, reverse=True)[LIMIT:])
print "files for remove: {0}".format(", ".join(remove_files))
for remove_file in remove_files:
os.remove(remove_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment