How to determine that if a folder is empty?

C

could ildg

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~
 
N

Neil Hodgson

could ildg:
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?

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
 
C

could ildg

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

could ildg:
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?

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
 
R

Reinhold Birkenfeld

could said:
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. A directory (please stop calling them "folders")
can only be removed if it's empty.

Reinhold
 
J

James Dennett

Reinhold said:
could said:
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. A directory (please stop calling them "folders")
can only be removed if it's empty.

Reinhold

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
 
N

Neil Hodgson

could ildg
so you mean that this is not platform-independent?

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
 
J

John Machin

Reinhold said:
could said:
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.
 
P

Peter Hansen

could said:
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...

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
 
M

Mike Meyer

James Dennett said:
Reinhold said:
could said:
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. A directory (please stop calling them "folders")
can only be removed if it's empty.
Reinhold

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

It's also buggy, in that there are other things that can cause an
OSError:
Traceback (most recent call last):

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
 
D

Dan Sommers

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?

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
 
D

David Cuthbert

Mike said:
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?

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

could ildg

could said:
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.

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_DEFLATED)
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."
 
P

Peter Hansen

could said:
could said:
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
 
S

Scott David Daniels

could said:
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....

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,986
Latest member
ColbyG935

Latest Threads

Top