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

Ethan Furman ethan at stoneleaf.us
Thu Aug 7 03:30:27 EDT 2014


On 08/06/2014 03:26 PM, Ben Finney wrote:
> Virgil Stokes <vs at it.uu.se> writes:
>
>> Suppose I have a directory C:/Test that is either empty or contains
>> more than 2000000 files, all with the same extension (e.g. *.txt). How
>> can I determine if the directory is empty WITHOUT the generation of a
>> list of the file names in it (e.g. using os.listdir('C:/Test')) when
>> it is not empty?
>
> What is your goal for that? Have you measured the performance difference
> and decided *based on objective observation* that it's too expensive?
>
> Certainly ‘os.listdir(foo)’ is the simplest way to determine the entries
> in a directory, and thereby to test whether it is empty. That simplicity
> is very valuable, and you should have a compelling, *measured* reason to
> do something more complicated. What is it?

Plenty of people have measured the slowdown of getting a list of files when the directory has thousands upon thousands. 
  It's why the scandir PEP exists at all, and the slowdown is present even on local file systems.  While it may not be 
objective, walking away to get a cup of your favorite beverage, coming back and seeing the operation is still not done, 
is certainly sufficient to realize that the simple, easy way is not going to be sufficient.

--
~Ethan~



More information about the Python-list mailing list