How to determine that if a folder is empty?
Scott David Daniels
Scott.Daniels at Acm.Org
Tue Aug 9 18:46:08 EDT 2005
could ildg wrote:
> 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
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list