Welcome, guest | Sign In | My Account | Store | Cart

This iterator can bee used to walk through directories.

Python, 32 lines
 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.

5 comments

Tony C 20 years, 1 month ago  # | flag

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] == ':':
Alex Martelli 19 years, 10 months ago  # | flag

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

Wai Yip Tung 19 years, 9 months ago  # | flag

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.

thattommyhall ; 18 years, 9 months ago  # | flag

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)

Gary Eakins 18 years ago  # | flag

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.
Created by Sébastien Keim on Wed, 26 Feb 2003 (PSF)
Python recipes (4591)
Sébastien Keim's recipes (24)

Required Modules

Other Information and Tasks