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

Akira Li 4kir4.1i at gmail.com
Thu Aug 7 08:19:19 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?

  def is_empty_dir(dirpath):
      return next(scandir(dirpath), None) is None

 https://github.com/benhoyt/scandir


 --
 Akira




More information about the Python-list mailing list