This iterator can bee used to walk through directories.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import os
class iterdir(object):
def __init__(self, path, deep=False):
self._root = path
self._files = None
self.deep = deep
def __iter__(self):
return self
def next(self):
if self._files:
join = os.path.join
d = self._files.pop()
r = join(self._root, d)
if self.deep and os.path.isdir(r):
self._files += [join(d,n) for n in os.listdir(r)]
elif self._files is None:
self._files = os.listdir(self._root)
if self._files:
return self._files[-1]
else:
raise StopIteration
# sample:
# a deep traversal of directories which starts with a vowel
#
it = iterdir('.')
for x in it:
p = os.path.basename(x)
it.deep = p[0].lower() in "aeiou"
print x
|
This class is intended to provide an iterator version of the os.listdir function. The interesting property of the iterator is that you can control if you want a deep iteration of directories with the 'deep' member.
After having returned the path of a directory, the iterator will yield the content of this directory if the deep member is true.
Tags: sysadmin
Dirwalk- does this code even work ? Has anyone tries this code ? I've tried running it on Windows XP, and Windows 98, both using Python 2.3. It doesn't run on my systems.
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bi Type "help", "copyright", "credits" or "license" for more i
there just seems to be some weird typo in the last two lines. The code is just fine, on Windows just as well as elsewhere, except for the last couple of lines in the "sample" part at the end which seem to be weirdly and hopelessly mangled. Just change the last 2 lines to:
and everything should work.
Alex
os.walk(). os.walk() does similar thing in Python 2.3
Before 2.3 there is a somewhat unintuitive os.path.walk() that needs a callback. This directory iterator is more straight forward compare to it.
look at the path module. http://www.jorendorff.com/articles/python/path/index.html has a module the presents file paths as iteratable objects. i think this does everything the script does and more. only disadvantage is that i think it uses generators so will only work on python inc them (2.3+ i think)
Worked for me, but not text.source. If you copy with the mouse and precede the indented sample with