Skip to content

Instantly share code, notes, and snippets.

@aanari
Created March 31, 2012 21:09
Show Gist options
  • Save aanari/2268531 to your computer and use it in GitHub Desktop.
Save aanari/2268531 to your computer and use it in GitHub Desktop.
Calibre Duplicate Folders
import os, stat, shutil, re
for author in [author for author in os.listdir('.') if os.path.isdir(os.path.join('.', author))]:
lastbook = {}
for book in [re.search('(?P<title>.*)\s?\((?P<id>\d+)\)', book).groupdict() for book in os.listdir(author + '/.') if os.path.isdir(os.path.join('.', author + '/' + book))]:
if book['title'] in lastbook:
if float(book['id']) > float(lastbook[book['title']]):
lastbook[book['title']] = book['id']
else:
lastbook[book['title']] = book['id']
test = [('%s(%s)' % (k, v)).replace(' ','') for k, v in lastbook.iteritems()]
for book in [book for book in os.listdir(author + '/.') if os.path.isdir(os.path.join('.', author + '/' + book))]:
if book.replace(' ','') not in test:
path = author + '/' + book
os.chmod(path, stat.S_IWRITE)
shutil.rmtree(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment