how to get number of images in a folder ( and its subfolders) with pathlib in python?
- User
how to get number of images in a folder ( and its subfolders) with pathlib in python?
- Assistant
You can use Python's built-in
pathlibmodule to traverse directories and count image files based on their file extensions. Here's how you can do it:from pathlib import Path # Define the folder path folder_path = Path('/path/to/your/folder') # Define the image file extensions you're interested in image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff'] # Use rglob to recursively search for image files image_files = [file for file in folder_path.rglob('*') if file.suffix.lower() in image_extensions] # Get the number of image files num_images = len(image_files) print(f'Total number of images: {num_images}')Explanation:
-
Import Path from pathlib: This class provides methods to manipulate filesystem paths in an object-oriented way.
-
Set the folder path: Replace
'/path/to/your/folder'with the path to the directory you want to search. -
Define image extensions: Create a list of the image file extensions you want to include. Make sure to include the dot (
.) before the extension and use lowercase for consistency. -
Recursively search for image files:
folder_path.rglob('*')recursively goes through all files and subdirectories in the given folder.- The list comprehension filters files by checking if their suffix (file extension) is in the
image_extensionslist. file.suffix.lower()ensures that the file extension is compared in lowercase, making the search case-insensitive.
-
Count the images:
len(image_files)gives you the total number of image files found. -
Print the result: Display the total number of images.
Alternative method using glob patterns:
If you prefer, you can use glob patterns for each image extension:
from pathlib import Path # Define the folder path folder_path = Path('/path/to/your/folder') # Define glob patterns for image files image_patterns = ['*.jpg', '*.jpeg', '*.png', '*.gif', '*.bmp', '*.tiff'] # Initialize an empty list to store image files image_files = [] # Loop through each pattern and extend the image_files list for pattern in image_patterns: image_files.extend(folder_path.rglob(pattern)) # Get the number of image files num_images = len(image_files) print(f'Total number of images: {num_images}')In this version:
-
Use glob patterns: Each image extension is used as a pattern in the
rglobmethod (e.g.,'*.jpg'). -
Extend the list of image files: As you loop through each pattern, use
extendto add found files to theimage_fileslist.
Note: The
rglobmethod is recursive and searches all subdirectories. If you only want to search the specified directory and not its subdirectories, you can useglobinstead ofrglob:# Use glob for non-recursive search image_files = [file for file in folder_path.glob('*') if file.suffix.lower() in image_extensions]Remember to replace
'/path/to/your/folder'with the actual path to your folder.By using
pathlib, you gain an efficient and readable way to interact with filesystem paths and perform file operations in Python. -