Created
May 10, 2023 18:00
-
-
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.
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 | |
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