I’d like to recursively walk a directory, but I want python to break from any single listdir if it encounters a directory with greater than 100 files. Basically, I’m searching for a (.TXT) file, but I want to avoid directories with large DPX image sequences (usually 10,000 files). Since DPXs live in directories by themselves with no sub directories, I’d like to break that loop ASAP.
So long story short, if python encounters a file matching “.DPX$” it stops listing the sub-directory, backs out, skips that sub-directory and continues the walk in other sub-directories.
Is this possible to break a directory listing loop before all the list results are returned?
The right way to avoid allocating the list of names using the os.listdir is to use the os level function as @Charles Duffy said.
Inspired from this other post: List files in a folder as a stream to begin process immediately
I added how to solve the specific OP question and used the re-entrant version of the function.