Connecting Tech Pros Worldwide Forums | Help | Site Map

How to determine that if a folder is empty?

could ildg
Guest
 
Posts: n/a
#1: Aug 8 '05
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?
Thanks~

Neil Hodgson
Guest
 
Posts: n/a
#2: Aug 8 '05

re: How to determine that if a folder is empty?


could ildg:
[color=blue]
> 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?[/color]

The first thing to do is measure the performance of this operation
in the context of your application to see if it is really worth extra
effort.
Tweaking this will depend on the platform. On Windows you can use
ctypes and the system functions FindFirstFile/FindNextFile/FindClose,
finishing once you see a single file. Directories always contain "." and
".." entries so they should be ignored. On other platforms there will be
similar low level functions.

Neil
could ildg
Guest
 
Posts: n/a
#3: Aug 8 '05

re: How to determine that if a folder is empty?


Thank you .
so you mean that this is not platform-independent?

On 8/8/05, Neil Hodgson <nyamatongwe+thunder@gmail.com> wrote:[color=blue]
> could ildg:
> [color=green]
> > 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?[/color]
>
> The first thing to do is measure the performance of this operation
> in the context of your application to see if it is really worth extra
> effort.
> Tweaking this will depend on the platform. On Windows you can use
> ctypes and the system functions FindFirstFile/FindNextFile/FindClose,
> finishing once you see a single file. Directories always contain "." and
> ".." entries so they should be ignored. On other platforms there will be
> similar low level functions.
>
> Neil
> --
> http://mail.python.org/mailman/listinfo/python-list
>[/color]
Reinhold Birkenfeld
Guest
 
Posts: n/a
#4: Aug 8 '05

re: How to determine that if a folder is empty?


could ildg wrote:[color=blue]
> 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?[/color]

try:
os.rmdir(path)
empty = True
except OSError:
empty = False

should be efficient. A directory (please stop calling them "folders")
can only be removed if it's empty.

Reinhold
James Dennett
Guest
 
Posts: n/a
#5: Aug 8 '05

re: How to determine that if a folder is empty?


Reinhold Birkenfeld wrote:
[color=blue]
> could ildg wrote:
>[color=green]
>>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?[/color]
>
>
> try:
> os.rmdir(path)
> empty = True
> except OSError:
> empty = False
>
> should be efficient. A directory (please stop calling them "folders")
> can only be removed if it's empty.
>
> Reinhold[/color]

Unfortunately that destroys the directory if it was empty,
and then you can't recreate it unless (a) you went to the
trouble of preserving all of its attributes before deleting
it, and (b) your script has OS-level permissions to recreate
the directory with its original attributes (such as owners
and permissions).

Also note that modifying a filesystem can be significantly
slower than reading from it (it varies widely across platforms).

-- James
Neil Hodgson
Guest
 
Posts: n/a
#6: Aug 8 '05

re: How to determine that if a folder is empty?


could ildg
[color=blue]
> so you mean that this is not platform-independent?[/color]

Your existing code is platform-independent but I think for faster
code you will need to write platform-dependent code. FindFirstFile is
only available on Windows.

Neil
John Machin
Guest
 
Posts: n/a
#7: Aug 8 '05

re: How to determine that if a folder is empty?


Reinhold Birkenfeld wrote:[color=blue]
> could ildg wrote:
>[color=green]
>>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?[/color]
>
>
> try:
> os.rmdir(path)
> empty = True
> except OSError:
> empty = False
>
> should be efficient.[/color]

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

Peter Hansen
Guest
 
Posts: n/a
#8: Aug 8 '05

re: How to determine that if a folder is empty?


could ildg wrote:[color=blue]
> 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?[/color]

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...

Reinhold's suggestion to delete the folder was interesting in this
respect... isn't that (prior to deleting a folder) just about the only
time one cares if it's empty, normally? And in this case you don't need
to do the check, as Reinhard shows, so performance isn't an issue.

-Peter
Mike Meyer
Guest
 
Posts: n/a
#9: Aug 8 '05

re: How to determine that if a folder is empty?


James Dennett <jdennett@acm.org> writes:[color=blue]
> Reinhold Birkenfeld wrote:
>[color=green]
>> could ildg wrote:
>>[color=darkred]
>>>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?[/color]
>> try:
>> os.rmdir(path)
>> empty = True
>> except OSError:
>> empty = False
>> should be efficient. A directory (please stop calling them "folders")
>> can only be removed if it's empty.
>> Reinhold[/color]
>
> Unfortunately that destroys the directory if it was empty,
> and then you can't recreate it unless (a) you went to the
> trouble of preserving all of its attributes before deleting
> it, and (b) your script has OS-level permissions to recreate
> the directory with its original attributes (such as owners
> and permissions).[/color]

It's also buggy, in that there are other things that can cause an
OSError:
[color=blue][color=green][color=darkred]
>>> import os
>>> os.rmdir("/tmp/x")[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 1] Operation not permitted: '/tmp/x'[color=blue][color=green][color=darkred]
>>> ^D[/color][/color][/color]

bhuda% bhuda% ls -a /tmp/x
.. ..

The problem here is that I don't own /tmp/x, so I can't delete it,
hence I get an OSError even though it's empty.

Just out of curiosity, is there an OS out there where you can have the
permissions needed to delete a directory without having the
permissions needed to create it appropriately?

<mike
--
Mike Meyer <mwm@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dan Sommers
Guest
 
Posts: n/a
#10: Aug 8 '05

re: How to determine that if a folder is empty?


On Mon, 08 Aug 2005 09:03:39 -0400,
Mike Meyer <mwm@mired.org> wrote:
[color=blue]
> Just out of curiosity, is there an OS out there where you can have the
> permissions needed to delete a directory without having the
> permissions needed to create it appropriately?[/color]

Depending on your definition of "out there," yes, some OS's grant those
privileges separately. Apollo (eventually bought out by HP) Aegis
(mostly like *nix, but just different enough to trip me up again and
again) used to be one. The more secure the environment, the more likely
certain privileges do *not* imply others.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
David Cuthbert
Guest
 
Posts: n/a
#11: Aug 8 '05

re: How to determine that if a folder is empty?


Mike Meyer wrote:[color=blue]
> Just out of curiosity, is there an OS out there where you can have the
> permissions needed to delete a directory without having the
> permissions needed to create it appropriately?[/color]

Not an OS, but AFS has a wider set of permissions (RLIDWKA - which, if I
remember correctly, are read, look, index, delete, write, lock,
administrate).
could ildg
Guest
 
Posts: n/a
#12: Aug 9 '05

re: How to determine that if a folder is empty?


On 8/8/05, Peter Hansen <peter@engcorp.com> wrote:[color=blue]
> could ildg wrote:[color=green]
> > 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?[/color]
>
> 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...[/color]
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.

import os,zipfile
from os.path import join
from datetime import date

def zipfolder(foldername,filename):
"""
zip folder foldername and all its subfiles and folders into a
zipfile named filename.
"""
zip=zipfile.ZipFile(filename,"w",zipfile.ZIP_DEFLA TED)
for root,dirs,files in os.walk(foldername):
for dir in dirs:
#now I don't check any more whether a dir is empty
zif=zipfile.ZipInfo(join(root,dir)+"/")
zip.writestr(zif,"")
for filename in files:
print "compressing ",join(root,filename)
zip.write(join(root,filename))
zip.close()
print "Finished compressing."

[color=blue]
>
> Reinhold's suggestion to delete the folder was interesting in this
> respect... isn't that (prior to deleting a folder) just about the only
> time one cares if it's empty, normally? And in this case you don't need
> to do the check, as Reinhard shows, so performance isn't an issue.
>
> -Peter
> --
> http://mail.python.org/mailman/listinfo/python-list
>[/color]
Peter Hansen
Guest
 
Posts: n/a
#13: Aug 9 '05

re: How to determine that if a folder is empty?


could ildg wrote:[color=blue]
> On 8/8/05, Peter Hansen <peter@engcorp.com> wrote:[color=green]
>>could ildg wrote:[color=darkred]
>>>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?[/color]
>>
>>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...[/color]
>
> 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.[/color]

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
Scott David Daniels
Guest
 
Posts: n/a
#14: Aug 9 '05

re: How to determine that if a folder is empty?


could ildg wrote:[color=blue]
> 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....[/color]

Can you make work some variant of:

wanted, stack = {}, ()
for root,dirs,files in os.walk('.'):
if files:
# Don't worry about parent dictionaries
for name in reversed(stack):
if name in wanted and (root.startswith(name) and
root[len(name)] == os.path.sep):
del wanted[name]
stack.pop()
else:
break
else:
wanted[root] = True
print root, dirs, len(files)
while len(stack) and not (root.startswith(stack[-1]) and
root[len(stack[-1])] == os.path.sep):
stack.pop()
stack.append(root)

--Scott David Daniels
Scott.Daniels@Acm.Org
Closed Thread