-
-
Save therealmarv/ec603bd4a91d51092a18 to your computer and use it in GitHub Desktop.
| #!/usr/bin/env python | |
| # Script: remove_jpg_if_raw_exists.py | |
| # | |
| # Description: This script looks in all sub directories for | |
| # pairs of JPG and RAW files. | |
| # For each pair found the JPG is moved to a | |
| # waste basket directory. | |
| # Otherwise JPG is kept. | |
| # | |
| # Author: Thomas Dahlmann | |
| # Modified by: Renaud Boitouzet | |
| import os | |
| import shutil | |
| # define your file extensions here, case is ignored. | |
| # Please start with a dot. | |
| # multiple raw extensions allowed, single jpg extension only | |
| raw_extensions = (".Dng", ".cR2", ".nef", ".crw") | |
| jpg_extension = ".jPg" | |
| # define waste basket directory here. Include trainling slash or backslash. | |
| # Windows : waste_dir = "C:\path\to\waste\" | |
| waste_dir = "/Users/marvin/Pictures/waste/" | |
| ##### do not modify below ########## | |
| # find files | |
| def locate(folder, extensions): | |
| '''Locate files in directory with given extensions''' | |
| for filename in os.listdir(folder): | |
| if filename.endswith(extensions): | |
| yield os.path.join(folder, filename) | |
| # make waste basket dir | |
| if not os.path.exists(waste_dir): | |
| os.makedirs(waste_dir) | |
| # Make search case insensitive | |
| raw_ext = tuple(map(str.lower,raw_extensions)) + tuple(map(str.upper,raw_extensions)) | |
| jpg_ext = (jpg_extension.lower(), jpg_extension.upper()) | |
| root=os.curdir | |
| #find subdirectories | |
| for path, dirs, files in os.walk(os.path.abspath(root)): | |
| print path | |
| raw_hash = {} | |
| for raw in locate(path, raw_ext): | |
| base_name = os.path.basename(raw) | |
| base_name = os.path.splitext(base_name)[0] | |
| raw_hash[base_name] = True | |
| # find pairs and move jpgs of pairs to waste basket | |
| for jpg in locate(path, jpg_ext): | |
| base_name = os.path.basename(jpg) | |
| base_name = os.path.splitext(base_name)[0] | |
| if base_name in raw_hash: | |
| jpg_base_name_with_ext = base_name + jpg_extension | |
| new_jpg = waste_dir + jpg_base_name_with_ext | |
| print "%s: %s = %s => %s" % (path, base_name, jpg, waste_dir) | |
| if os.path.exists(new_jpg): | |
| os.remove(jpg) | |
| else: | |
| shutil.move(jpg, new_jpg) |
on line 60, I'm getting a syntax error at the close quote (") immediately following the last %s. I have not solved that issue yet. Script won't run because of this issue.
A little bit later but...
This is the correct 60 line:
print ("{}: {} = {} => {}".format(path, base_name, jpg, waste_dir))
To all of those who wrote and fine-tuned this script, thank you. It was exactly what I was looking for and it worked perfectly.
for python 3 line 46 should be: print(path)
EDIT: oh sorry goremanX already mentioned that 🙈
Thanks @tazztone. It was written when Python 2 was widely used. I will fix it when at home.
yea would be nice if upgraded to python3. idk how to code, i just asked chatgpt what might be wrong with the script.
changed line 24 to waste_dir = r"X:\2023\waste" so that it doesn't create a folder named "3" (coz apparently \202 is interpreted as "octal escape"?)
while running it i found that the waste-jpgs get moved beside "waste" folder, instead of inside it.
idk maybe this helps u to fix with less effort.
there's no mention of which Python version this is for, so I assumed version 3.
When setting the "waste" folder in Windows, the \ character needs to be escaped with another \
For example: d:\path\to\waste
on line 46, the print function should use parentheses, so print(path) instead of print path
on line 60, I'm getting a syntax error at the close quote (") immediately following the last %s. I have not solved that issue yet. Script won't run because of this issue.