[Tutor] Directory not empty :: Fast mode

Max Noel maxnoel_fr at yahoo.fr
Mon May 16 19:57:47 CEST 2005


On May 16, 2005, at 18:30, Jonas Melian wrote:

> To knowing if a directory isn't empty, I use:
>
> dir_example = "/usr/foo/"
>
> if glob.glob(os.path.join(dir_example, "*")):
>     ...
>
>
> But, as it has to expand all files, i'm supposed that it's a little  
> slow
> to simply knowing if a directory is empty.
>
> Any improvement?
>

     The os.listdir() function returns a list of all the elements  
(files and dirs, everything but . and ..) in a directory. So if it  
returns an empty list, your dir is empty.

 >>> import os
 >>> os.listdir("Prog")
['.DS_Store', 'archive', 'C-Cpp', 'docs', 'eclipse', 'gi21',  
'Haskell', 'Java', 'matt', 'ObjC', 'Python']


-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting  
and sweating as you run through my corridors... How can you challenge  
a perfect, immortal machine?"



More information about the Tutor mailing list