ActiveState Code

Recipe 184602: Directory Iterator


This iterator can bee used to walk through directories.

Python
 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

Discussion

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.

Comments

  1. 1. At 8:36 p.m. on 14 mar 2004, Tony C said:

    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

    >>> import dirwalk
    Traceback (most recent call last):
      File "", line 1, in ?
      File "dirwalk.py", line 38, in ?
        p = os.path.basename(x)
      File "C:\Python23\Lib\ntpath.py", line 199, in basename
        return split(p)[1]
      File "C:\Python23\Lib\ntpath.py", line 163, in split
        d, p = splitdrive(p)
      File "C:\Python23\Lib\ntpath.py", line 118, in splitdrive
        if p[1:2] == ':':
    
  2. 2. At 3:46 a.m. on 28 jun 2004, Alex Martelli said:

    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:

    if p[0].lower() in 'aeiou':
        print x
    

    and everything should work.

    Alex

  3. 3. At 3:40 p.m. on 29 jun 2004, Wai Yip Tung said:

    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.

  4. 4. At 3:52 p.m. on 11 jul 2005, thattommyhall ; said:

    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)

  5. 5. At 6:12 p.m. on 14 apr 2006, Gary Eakins said:

    Worked for me, but not text.source. If you copy with the mouse and precede the indented sample with

        if __name__ == "__main__":
    
    The text.source version has tabs|spaces mixture and needs much
    editing to get right.
    

Sign in to comment