I use os.walk to select files from a specific folder that match a regular expression.
for dirpath, dirs, files in os.walk(str(basedir)): files[:] = [f for f in files if re.match(regex, os.path.join(dirpath, f))] print dirpath, dirs, files
But this should handle all the files and folders under control, which is quite time consuming. I am looking for a way to use the same regular expression used for files to filter out unwanted directories at every step of the walk. Or a way to match only part of a regular expression ...
For example, in a type structure
/data/2013/07/19/file.dat
using for example the following regular expression
/data/(?P<year>2013)/(?P<month>07)/(?P<day>19)/(?P<filename>.*\.dat)
find all .dat files without having to search, for example. / data / 2012
source share