Changing a value for each folder while traversing a file system
Peter Otten
__peter__ at web.de
Thu Jul 27 09:24:35 EDT 2006
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
More information about the Python-list
mailing list