Created
July 14, 2023 20:53
-
-
Save derekslenk/0b39e92cc6e3e87b286099466aab71a9 to your computer and use it in GitHub Desktop.
I had a folder of printing models that all had images, but were all in individual subfolders and named funny. This pulls out the name of all the image files in a list so you can browse/do with as you please
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 sys | |
import re | |
from pathlib import Path | |
# Copyright (c) 2023 Derek Slenk. All rights reserved. | |
INVALID_FILENAME_PATTERN = re.compile(r"\d+[\w]+\.\w+") | |
ALLOWED_CHARS_PATTERN = re.compile(r"[^\d_-]") | |
def is_valid_filename(filename): | |
return not INVALID_FILENAME_PATTERN.fullmatch(filename) | |
def is_allowed_chars(filename): | |
return not ALLOWED_CHARS_PATTERN.search(filename) | |
def find_image_files(root_path, output_file): | |
image_extensions = {".jpg", ".jpeg", ".png"} | |
directories = set() | |
for file in Path(root_path).rglob("*"): | |
if file.is_file() and file.suffix.lower() in image_extensions and is_valid_filename(file.name): | |
if is_allowed_chars(file.stem): | |
directories.add(file.parent.name) | |
else: | |
output_file.write(file.name + "\n") | |
for directory in directories: | |
output_file.write(directory + "\n") | |
def main(): | |
if len(sys.argv) < 2: | |
print("Please provide the root path as a command-line argument.") | |
return | |
root_path = sys.argv[1] | |
output_path_raw = Path.cwd() / "list-raw.txt" | |
with output_path_raw.open("w") as output_file: | |
find_image_files(root_path, output_file) | |
print(f"File names and directory names saved to {output_path_raw}") | |
sorted_output_filename = "list-adjusted.txt" | |
sorted_output_path = Path.cwd() / sorted_output_filename | |
with output_path_raw.open("r") as input_file, sorted_output_path.open("w") as sorted_file: | |
lines = sorted(set(input_file.readlines())) | |
sorted_file.writelines(lines) | |
print(f"Sorted file names and directory names saved to {sorted_output_path}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment