Changing a value for each folder while traversing a file system

PipedreamerGrey pipedreamergrey at gmail.com
Fri Jul 28 10:45:45 EDT 2006


Perfect.  That's exactly what I wanted.  Thanks.

For those reading this later on, the following script will crawl
through a directory, select all the text files, dump them into seperate
numbered html files in the parent directory (in which the python script
is executed).  In this example, the first line of the text file is
placed into a table with a randomly colored background.  With a little
work, the text files can be complexly formatted, each folder can be
color coded, etc.

#! /usr/bin/python
import glob
import fileinput
import os
import string
import sys

count == 1
number == 1

class DirectoryWalker:
    # a forward iterator that traverses a directory tree, and
    # returns the filename

    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:
                # get a filename, eliminate directories from list
                fullname = os.path.join(self.directory, file)
                if os.path.isdir(fullname) and not
os.path.islink(fullname):
                    self.stack.append(fullname)
                else:
                    return fullname

for file in DirectoryWalker("."):
 last_directory = None
for file in DirectoryWalker("."):
    issue = number +".html"
    directory = os.path.dirname(file)
    if directory != last_directory:
        color = random.choice(["#990000", "#009900", "#000099"])
        last_directory = directory

    # divide files names into path and extention
    path, ext = os.path.splitext(file)
    # choose the extention you would like to see in the list
    if ext == ".txt":
        print file
        file = open(file)
        fileContent = file.readlines()
       # just for example, let's say I want to print the color here as
if in an html tag...
        issue.write("<html><head></head><body>")
        for line in fileContent:
            if not line.startswith("\n"):
	    if count == 1:
	        issue.write('<table bgcolor="'+color+'" width="100%"
border="0" cellspacing="0" cellpadding="0"><tr><td>')
                    issue.write(line)
                    issue.write("</td></tr></table>")
	        count = count + 1
                else:
	        issue.write("<p>")
                    issue.write(line)
                    issue.write("</p>")
        issue.write("</body></html>")    
issue.close()




More information about the Python-list mailing list