Re: Changing a value for each folder while traversing a file system
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([#99, #009900, #99]) 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(htmlhead/headbody) for line in fileContent: if not line.startswith(\n): if count == 1: issue.write('table bgcolor='+color+' width=100% border=0 cellspacing=0 cellpadding=0trtd') 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() -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing a value for each folder while traversing a file system
No, that doesn't work. Though, leaving the random snippet about the for file in DirectoryWalker(.): line, it does leave all files the same value, rather than switching the value for every single file. The problem is, the value is shared accross every folder. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing a value for each folder while traversing a file system
PipedreamerGrey wrote: I'm using the script below (originally from http://effbot.org, given to me here) to open all of the text files in a directory and its subdirectories and combine them into one Rich text file (index.rtf). Now I'm adapting the script to convert all the text files into individual html files. What I can't figure out is how to trigger a change in the background value for each folder change (not each file), so that text is color-coded by folder. for file in DirectoryWalker(.): # 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... index.write(color) for line in fileContent: if not line.startswith(\n): index.write(line) index.write(\n) You have to remember which directory you are in: #untested last_directory = None for file in DirectoryWalker(.): directory = os.dirname(file) if directory != last_directory: color = random.choice([red, green, blue]) last_directory = directory # do whatever you want with file and color print color, file Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing a value for each folder while traversing a file system
That seems logical, but the syntax doesn't work. I keep getting the error: 'module object' has no attribute 'dirname' -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing a value for each folder while traversing a file system
PipedreamerGrey wrote: That seems logical, but the syntax doesn't work. I keep getting the error: 'module object' has no attribute 'dirname' os.dirname() was a typo, try os.path.dirname(). Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing a value for each folder while traversing a file system
PipedreamerGrey wrote: I'm using the script below (originally from http://effbot.org, given to me here) to open all of the text files in a directory and its subdirectories and combine them into one Rich text file (index.rtf). Now I'm adapting the script to convert all the text files into individual html files. What I can't figure out is how to trigger a change in the background value for each folder change (not each file), so that text is color-coded by folder. I have tried to integrate the following snippet into the directory walker, so that I could later write it to the file as text: color = random.choice([#99, #CC, #99]) That method didn't work, does anyone else have a suggestion? #! /usr/bin/python import glob import fileinput import os import string import sys index = open(index.rtf, 'w') 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(.): # 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... index.write(color) for line in fileContent: if not line.startswith(\n): index.write(line) index.write(\n) index.close() Add a color attribute to the DirectoryWalker class, change it when you change directories, return it with the fullname. Change your for loop like so: for color, file in DirectoryWalker(.): # ... I think that should work. HTH, ~Simon -- http://mail.python.org/mailman/listinfo/python-list