By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

Changing a value for each folder while traversing a file system

P: n/a
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(["#990000", "#CCCCCC", "#000099"])
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()

Jul 26 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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(["#990000", "#CCCCCC", "#000099"])
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

Jul 26 '06 #2

P: n/a
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.

Jul 27 '06 #3

P: n/a
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

Jul 27 '06 #4

P: n/a
That seems logical, but the syntax doesn't work. I keep getting the
error:
'module object' has no attribute 'dirname'

Jul 27 '06 #5

P: n/a
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
Jul 27 '06 #6

P: n/a
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()

Jul 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.