Skip to content

Instantly share code, notes, and snippets.

@petersgiles
Last active January 15, 2023 20:38
Show Gist options
  • Save petersgiles/b8031736933b303af96bda927779cb3f to your computer and use it in GitHub Desktop.
Save petersgiles/b8031736933b303af96bda927779cb3f to your computer and use it in GitHub Desktop.
Dedup Itunes library
# Please be very careful while using this script, as the os.remove(file_path) command will delete the file permanently.
# Make sure you have a backup of your iTunes library before running this script.
# Also, this script will only prompt the user for deleting the duplicate files in the album that contains duplicate files.
import os
import hashlib
itunes_library = "/path/to/iTunes/Library"
# Create a dictionary to store file hashes
files = {}
albums = {}
for root, dirs, filenames in os.walk(itunes_library):
for filename in filenames:
file_path = os.path.join(root, filename)
# Calculate the MD5 hash of the file
hash = hashlib.md5()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash.update(chunk)
file_hash = hash.hexdigest()
# get the album name
album_name = os.path.basename(root)
if album_name not in albums:
albums[album_name] = set()
# If the file hash is already in the dictionary, save it to the album
if file_hash in files:
albums[album_name].add(file_path)
files[file_hash].add(file_path)
else:
files[file_hash] = {file_path}
# go through all the albums and prompt user if they want to delete the duplicate files
for album_name, duplicate_files in albums.items():
if len(duplicate_files) == 0:
continue
print(f"Duplicate files found in {album_name} : {duplicate_files}")
user_input = input("Do you want to delete these files? (y/n)")
if user_input.strip().lower() == "y":
for file_path in duplicate_files:
os.remove(file_path)
print(f"Deleted {file_path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment