Created
March 31, 2012 21:09
-
-
Save aanari/2268531 to your computer and use it in GitHub Desktop.
Calibre Duplicate Folders
This file contains 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 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