Skip to content

Instantly share code, notes, and snippets.

@grauschnabel
Created September 17, 2024 09:58
Show Gist options
  • Save grauschnabel/fd46662dbb693dc95664713a0ad8f31f to your computer and use it in GitHub Desktop.
Save grauschnabel/fd46662dbb693dc95664713a0ad8f31f to your computer and use it in GitHub Desktop.
Removeing my darktable xmp files after removing images with another tool.
#!/usr/bin/env python
import os
def delete_unmatched_xmp_files(directory):
"""
Recursively delete .xmp files in a directory if the corresponding base file (e.g., .jpg, .png, .nef) does not exist.
:param directory: The directory to search for .xmp files.
"""
for item in os.listdir(directory):
item_path = os.path.join(directory, item)
# If it's a directory, recursively call this function
if os.path.isdir(item_path):
delete_unmatched_xmp_files(item_path)
# If it's an .xmp file, check if the corresponding base file exists
elif item.endswith(".xmp"):
base_name = os.path.splitext(item)[0] # Filename without .xmp extension
# If the base file doesn't exist, delete the .xmp file
if not os.path.exists(os.path.join(directory, base_name)):
print(f"Removing {item_path}, because {base_name} is missing.")
os.remove(item_path)
# Start the script in the current working directory
current_directory = os.getcwd()
delete_unmatched_xmp_files(current_directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment