Created
June 19, 2024 21:51
-
-
Save cgranier/d9942a9c50b5167b953a7a89ffb1b700 to your computer and use it in GitHub Desktop.
Traverse directories, identify the thumbnail images and their corresponding original images, and perform either a dry run or a hot run to delete the thumbnails
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 argparse | |
def find_thumbnails(directory): | |
thumbnails = [] | |
for root, _, files in os.walk(directory): | |
for file in files: | |
if file.endswith(".thumbnail.png") or file.endswith(".thumbnail.jpg"): | |
original_file = file.replace(".thumbnail", "") | |
if original_file in files: | |
thumbnails.append(os.path.join(root, file)) | |
return thumbnails | |
def dry_run(thumbnails): | |
print("Dry Run: Identifying thumbnail files to be deleted") | |
for thumbnail in thumbnails: | |
print(thumbnail) | |
def hot_run(thumbnails): | |
print("Hot Run: Deleting thumbnail files") | |
for thumbnail in thumbnails: | |
os.remove(thumbnail) | |
print(f"Deleted: {thumbnail}") | |
def main(): | |
parser = argparse.ArgumentParser(description="Find and delete thumbnail images") | |
parser.add_argument("directory", help="Directory to search for thumbnail images") | |
parser.add_argument("--dry-run", action="store_true", help="Perform a dry run (default)") | |
parser.add_argument("--hot-run", action="store_true", help="Perform a hot run (delete files)") | |
args = parser.parse_args() | |
thumbnails = find_thumbnails(args.directory) | |
if args.hot_run: | |
hot_run(thumbnails) | |
else: | |
dry_run(thumbnails) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment