Changing a value for each folder while traversing a file system

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PipedreamerGrey

    #1

    Changing a value for each folder while traversing a file system

    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__(sel f, 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(se lf.directory, file)
    if os.path.isdir(f ullname) and not
    os.path.islink( fullname):
    self.stack.appe nd(fullname)
    else:
    return fullname

    for file in DirectoryWalker ("."):
    # divide files names into path and extention
    path, ext = os.path.splitex t(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(col or)
    for line in fileContent:
    if not line.startswith ("\n"):
    index.write(lin e)
    index.write("\n ")

    index.close()

  • Simon Forman

    #2
    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(["#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__(sel f, 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(se lf.directory, file)
    if os.path.isdir(f ullname) and not
    os.path.islink( fullname):
    self.stack.appe nd(fullname)
    else:
    return fullname
    >
    for file in DirectoryWalker ("."):
    # divide files names into path and extention
    path, ext = os.path.splitex t(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(col or)
    for line in fileContent:
    if not line.startswith ("\n"):
    index.write(lin e)
    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

    Comment

    • PipedreamerGrey

      #3
      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.

      Comment

      • Peter Otten

        #4
        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.splitex t(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(col or)
        for line in fileContent:
        if not line.startswith ("\n"):
        index.write(lin e)
        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

        Comment

        • PipedreamerGrey

          #5
          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'

          Comment

          • Peter Otten

            #6
            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

            Comment

            • PipedreamerGrey

              #7
              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__(sel f, 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(se lf.directory, file)
              if os.path.isdir(f ullname) and not
              os.path.islink( fullname):
              self.stack.appe nd(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.splitex t(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("<h tml><head></head><body>")
              for line in fileContent:
              if not line.startswith ("\n"):
              if count == 1:
              issue.write('<t able bgcolor="'+colo r+'" width="100%"
              border="0" cellspacing="0" cellpadding="0" ><tr><td>')
              issue.write(lin e)
              issue.write("</td></tr></table>")
              count = count + 1
              else:
              issue.write("<p >")
              issue.write(lin e)
              issue.write("</p>")
              issue.write("</body></html>")
              issue.close()

              Comment

              Working...