Skip to content

Instantly share code, notes, and snippets.

@Edwardtonnn
Created January 11, 2024 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Edwardtonnn/ec8c01e8cbfac0be3b67148fb40f0b85 to your computer and use it in GitHub Desktop.
Save Edwardtonnn/ec8c01e8cbfac0be3b67148fb40f0b85 to your computer and use it in GitHub Desktop.
Python script to search a dirctory of galleries to find an even number of images. If dir does not have an even number of images it deletes the last one. The idea behind this script was to find trash images since before and after are always even.
import os
import glob
# Replace this with your directory path
base_directory = "./body/corrective-body-contouring/"
# Loop through each folder in the directory
for folder in os.listdir(base_directory):
folder_path = os.path.join(base_directory, folder)
# Skip if not a directory
if not os.path.isdir(folder_path):
continue
# Find all jpg files in the folder
images = glob.glob(os.path.join(folder_path, "*.jpg"))
# Check if the number of images is odd
if images and len(images) % 2 != 0:
# Sort the images to find the last one
images.sort()
last_image = images[-1]
# Delete the last image
os.remove(last_image)
print(f"Deleted {last_image}")
else:
print(f"Directory {folder} has an even number of images or no images. No action taken.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment