Test for an empty directory that could be very large if it is not empty?

Tim Chase python.list at tim.thechases.com
Thu Aug 7 07:07:37 EDT 2014


On 2014-08-07 11:27, Ben Finney wrote:
> > The difference in timings when serving a web-request are
> > noticeable (in my use-case, I had to change my algorithm and
> > storage structure to simplify/avoid heavily-populated
> > directories)  
> 
> So, if the requirement is “test whether the directory is empty
> faster than N microseconds”, that's quite different from “without
> the generation of a list of the file names”.
> 
> The former may entail the latter, but that's not to be assumed, and
> chasing an optimisation prematurely is a common cause of terrible
> code.

I guess my surprise in the 2->3 non-iterator'ization of
os.listdir() is that it's very easy to wrap an iterable in list()
if you want the whole bunch, but it's much harder to get the
performance characteristics of interruptible iteration (e.g. "is the
directory empty" or "look at files until you find one matching
$CRITERIA").  Looking forward to scandir() arriving for just those
reasons.

-tkc
(who sees Ethan Furman's excellent followup-post as I'm about to hit
Send)



More information about the Python-list mailing list