Skip to content

Instantly share code, notes, and snippets.

@sonnguyen9800
Created May 10, 2023 18:00
Show Gist options
  • Save sonnguyen9800/a74ce19310143eaefd58b0f2b9183170 to your computer and use it in GitHub Desktop.
Save sonnguyen9800/a74ce19310143eaefd58b0f2b9183170 to your computer and use it in GitHub Desktop.
Remove duplicate books (book folder) in a book directory. Require you to use "Find Duplicate" plugin first.
import os
import shutil
# Script to remove duplicate books in Calibre
# 1. Use Calibre's "Find Duplicate" plugin & run check (binary check) to remove all binary files that are duplicated
# 2. Run this script, point to the book directory.
# How it works?
# - Duplicated folders have no ebook format. They only has 2 files: image (book cover) & metadata files.
# - This script purge all folders that has less than or equal to 2 files.
#
directory = "C:YOUR_PATH_TO_DIRECTORY"
allFolders = os.listdir(directory)
tobedeletefolder_list = []
for AuthorDirectory in os.listdir(directory):
path_author_dir = os.path.join(directory, AuthorDirectory)
if (os.path.isdir(path_author_dir) == False):
continue
print("Author: " +path_author_dir)
for bookDirectory in os.listdir(path_author_dir):
book_directory_path = os.path.join(path_author_dir, bookDirectory)
#print("Book:" + book_directory_path)
allfiles = os.listdir(book_directory_path);
if (len(allfiles)> 2):
continue
if os.path.isdir(book_directory_path):
print("Book to be deleted:" + book_directory_path)
tobedeletefolder_list.append(book_directory_path)
for path in tobedeletefolder_list:
print("About to delete: " + path)
shutil.rmtree(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment