could ildg wrote: > On 8/8/05, Peter Hansen <[EMAIL PROTECTED]> 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? >> >>I'm just curious to know under what circumstances where it's important >>to know whether a directory is empty it's also important that the >>operation occur with lightning speed... > > I want to know this because I want to zip a directory and all of its > sub-files and sub-directories to a zip file. zipfile module of python > will not automatically include the empty directories, so I have to > check if a dir is empty and do this manually. I did this with java, > it's very fast, but when I do this with python. I use the code to > backup a directory every morning after I get up. It is not import if > it's fast or not. I just want to know whether their is better > solutions.
Thanks for the reply. I'll only point out, in case it's not now obvious, that in a situation where you are already going to be compressing (or have just compressed) a potentially large number of files in a potentially not empty subdirectory, the extremely slight extra cost of another os.listdir() will *never* be seen in the overall runtime of the code. Note also that any decent OS will have some sort of caching of directory info going on, so os.listdir() will probably do very little actual disk access and you shouldn't bother trying to optimize it away. -Peter -- http://mail.python.org/mailman/listinfo/python-list