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

Cameron Simpson cs at zip.com.au
Thu Aug 7 03:08:55 EDT 2014


On 07Aug2014 18:14, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>Virgil Stokes wrote:
>>How can I determine if the directory is empty WITHOUT the generation 
>>of a list of the file names
>
>Which platform?
>
>On Windows, I have no idea.
>
>On Unix you can't really do this properly without access
>to opendir() and readdir(), which Python doesn't currently
>wrap. [...]

On UNIX (the OP seemed to be using Windows, alas), if you are prepared to be 
destructive you can just do an rmdir. It will fail if the directory is not 
empty, and performs ok on the hypothesised remote once-ginormous directory.

The commonest reason for wanting to know if a directory is empty that I can 
imagine is when you want to remove it, and if that applies here it is better to 
just try to remove it and ask for forgiveness later.

Disclaimer: Windows may not offer this handy safety net.

Cheers,
Cameron Simpson <cs at zip.com.au>

Of course no description of a Ducati engine is complete without mentioning
the sound.  That deep bass exhaust rumble along with the mechanical music of
the desmodromic valves...too bad it isn't available on compact disc.
- Sport Rider, evidently before the release of the "Ducati Passions" CD



More information about the Python-list mailing list