Tag Archives: function

Python Snippet: Search for Files Recursively

Currently I’m working on a script to search for duplicate images on my hard drive. To do so, I need to search recursively and go down through all subdirectories.

Recursive searching can be done by importing the commonly used os module. Use the os.walk() function to recursively enumerate file names and paths in the supplied directory. There are then many ways to identify file types, but in this case I’m using fnmatch. Here’s the official fnmatch description:

This module provides support for Unix shell-style wildcards, which are not the same as regular expressions (which are documented in the re module).

We have the modules to use, now the code:

This function, called find_images(), returns an array of full file paths within a given directory that have a file type of ‘.jpg’. While searching, once an image is found we join the image name with root, the current directory being searched by os.walk(). This gives us back a list of full paths instead of just file names.

Example usage:

Hopefully I’ve explained the code clearly, but if not let me know. If you have any questions at all please feel free to ask in the comments.

Whenever you’re reading this, I hope you’re having a great day!