Skip to content

Instantly share code, notes, and snippets.

@felix-kolbe
Last active September 1, 2019 19:28
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 felix-kolbe/50ac00d6cf1cd1de3485820fffa884a8 to your computer and use it in GitHub Desktop.
Save felix-kolbe/50ac00d6cf1cd1de3485820fffa884a8 to your computer and use it in GitHub Desktop.
Tool to find remains of digital camera image file pairs (raw and jpg) and offer moving or deleting.
import argparse
import os
from pathlib import Path
path1 = r"c:\Users\Felix\Pictures\NX500c\_for_renaming\USA-2019"
EXTENSIONS_JPG = {'.JPG', '.JPEG'}
EXTENSIONS_RAW = {'.SRW', '.NRW'}
EXTENSIONS = EXTENSIONS_JPG | EXTENSIONS_RAW
# @dataclass
# class Image:
# pass
class SingleRawFinder:
def __init__(self, path: str):
assert os.path.exists(path) and os.path.isdir(path), "Given path is not a directory!"
self.path = Path(path)
assert self.path.exists() and self.path.is_dir(), "Given path is not a directory!"
print(self.path, self.path.absolute())
def scan(self):
# print(EXTENSIONS)
name_ext_file_tuples = []
for child in self.path.iterdir():
extension = child.suffix
# print(child, extension)
if extension.upper() in EXTENSIONS:
name = child.stem
name_ext_file_tuples.append((name, extension, child))
# print("\n".join(map(str, name_ext_file_tuples)))
self.name_ext_file_tuples = name_ext_file_tuples
def analyze(self):
name_to_list_of_tuples_dict = {}
for name_ext_file_tuple in self.name_ext_file_tuples:
(name, ext, path) = name_ext_file_tuple
if name not in name_to_list_of_tuples_dict:
name_to_list_of_tuples_dict[name] = []
name_to_list_of_tuples_dict[name].append(name_ext_file_tuple)
# print(name_to_list_of_tuples_dict)
# print("\n".join(name + " > " + " + ".join(path.name for (name, ext, path) in lot)
# for (name, lot) in name_to_list_of_tuples_dict.items()))
self.name_to_list_of_tuples_dict = name_to_list_of_tuples_dict
single_files_list = []
for (name, lot) in name_to_list_of_tuples_dict.items():
if len(lot) == 1:
(name, ext, path) = lot[0]
single_files_list.append(path)
# print(single_files_list)
self.single_files_list = single_files_list
def single_files_list_generator(self):
for file in self.single_files_list:
yield file
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="""Tool to find remains of digital camera image file pairs
(raw and jpg) and offer moving or deleting.""",
epilog="Currently following file name extensions are detected: "+
" ".join(EXTENSIONS) + " - case insensitive!")
parser.add_argument("path", help="Path in which to search for file pairs and singles.", nargs='?', default="test")
parser.add_argument("-r", "--recursive", help="Include subdirectories of given path(s)", action="store_true")
parser.add_argument("-print", action="store_true")
args = parser.parse_args()
print(args) # print(vars(args))
# print(path1)
assert not args.recursive, "Recursive search not yet implemented"
# srf = SingleRawFinder(path1)
srf = SingleRawFinder('.' if args.path is None else args.path)
srf.scan()
srf.analyze()
print("List of single files:")
for file in srf.single_files_list_generator():
print(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment