def sort_images(source, destination, structure, use_modified_date=False): """Sort images""" if not os.path.exists(source): raise Exception("Source directory does not exist.") # Walk through the source looking for new images for root, folders, files in os.walk(source): for file in files: full_path = root.rstrip("/") + "/" + file.lstrip("/") # Ignore directories if os.path.isdir(full_path): continue # Create a ImageFile wrapper object for the file imagefile = ImageFile( full_path, use_modified_date=use_modified_date) # If no date is found ignore the file date = imagefile.get_timestamp() if date is None: continue # Generate destination path destination_path = "{}/{}/".format( destination.rstrip("/"), date.strftime(structure)) # Move file imagefile.move(destination_path) print("Moved {} to {}.\n".format( full_path, imagefile.file_path))