Walking recursivly through a directory-structure
Alex
cut_me_out at hotmail.com
Mon May 29 11:41:20 EDT 2000
I wanted to learn to use os.path.walk, too, so I wrote a little function
that takes a directory and makes a graph out of all the files and
sub-directories below it:
Hope this helps.
Alex.
import os
class Directory:
def __init__ (self, path):
self.path = path
self.directory = {self.path: {}}
self.directory_pointers = {self.path: self.directory[self.path]}
os.path.walk (self.path, self.walk_callback, None)
def walk_callback (self, args, directory, files):
directory_list = self.directory_pointers[directory]
self.directory_pointers[directory] = directory_list
for file in files:
file = os.path.join (directory, file)
directory_list[file] = {}
if os.path.isdir (file):
self.directory_pointers[file] = directory_list[file]
if __name__ == '__main__':
t = Directory ('/afs/athena.mit.edu/user/a/l/alex_c/mail')
import pprint
pprint.pprint (t.directory)
More information about the Python-list
mailing list