How to determine that if a folder is empty?

John Machin sjmachin at lexicon.net
Mon Aug 8 08:04:35 EDT 2005


Reinhold Birkenfeld wrote:
> could ildg wrote:
> 
>>I want to check if a folder named "foldername" is empty.
>>I use os.listdir(foldername)==[] to do this,
>>but it will be very slow if the folder has a lot of sub-files.
>>Is there any efficient ways to do this?
> 
> 
> try:
>     os.rmdir(path)
>     empty = True
> except OSError:
>     empty = False
> 
> should be efficient.

But it removes the folder if it is empty. Perhaps it's not efficient to 
create the folder again.




More information about the Python-list mailing list