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

Ben Finney ben+python at benfinney.id.au
Wed Aug 6 18:26:49 EDT 2014


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?

-- 
 \         “The most dangerous man to any government is the man who is |
  `\       able to think things out for himself, without regard to the |
_o__)          prevailing superstitions and taboos.” —Henry L. Mencken |
Ben Finney




More information about the Python-list mailing list