Help me understand this iterator



Hi,

I've found this script over at effbot
(http://effbot.org/librarybook/os-path.htm), and I can't get my head
around its inner workings. Here's the script:

import os

class DirectoryWalker:
# a forward iterator that traverses a directory tree

def __init__(self, directory):
self.stack = [directory]
self.files = []
self.index = 0

def __getitem__(self, index):
while 1:
try:
file = self.files[self.index]
self.index = self.index + 1
except IndexError:
# pop next directory from stack
self.directory = self.stack.pop()
self.files = os.listdir(self.directory)
self.index = 0
else:
# got a filename
fullname = os.path.join(self.directory, file)
if os.path.isdir(fullname) and not
os.path.islink(fullname):
self.stack.append(fullname)
return fullname

for file in DirectoryWalker("."):
print file

Now, if I look at this script step by step, I don't understand:
- what is being iterated over (what is being called by "file in
DirectoryWalker()"?);
- where it gets the "index" value from;
- where the "while 1:"-loop is quitted.

Thanks in advance,

Mathieu

.



Relevant Pages

  • Re: Help me understand this iterator
    ... I've found this script over at effbot ... # a forward iterator that traverses a directory tree ... fullname = os.path.join ...
    (comp.lang.python)
  • Re: Running long script in the background
    ... I am trying to write a python cgi that calls a script over ssh, ... def command: ... iterator. ...
    (comp.lang.python)
  • Re: Really slow to get information from Win32_UserAccount
    ... This should get the fullname of current user (local as well as ... WMI provider for the Win32_UserAccount class could avoid having to ... The only way to find out really, is to try my script and see what ... If you mean all the machines that would run the code then no, some of them could be Windows NT 4.0 or Windows 9x.. ...
    (microsoft.public.win32.programmer.wmi)
  • RE: User Account "Full Name"....
    ... This posting is provided "AS IS" with no warranties, and confers no rights. ... Subject: User Account "Full Name".... ... I have a script as such ... via a script so I have the FullName value populated with a specific ...
    (microsoft.public.windows.server.active_directory)
  • Re: simple login script issue
    ... name and password and logs them in if the information is correct. ... This script is called auth.php as in included in all of the pages I ... example say Welcome $fullname when the only two fields ive currently ... Example SELECT * FROM messages WHERE userid = $userid ORDER by ...
    (alt.php)