tree.py

Go to the documentation of this file.
00001 ##
00002 # 
00003 # This module defines the Tree() function.  This function will return 
00004 # a list of strings that represent the directory tree for the directory
00005 # passed into the function.  The calling syntax is:
00006 # 
00007 #     Tree(dir, indent, leading_char)
00008 # 
00009 # The variable indent controls how much each subdirectory is indented
00010 # on each line.  The variable leading_char sets the leading character
00011 # in the list; '|' might not be a bad choice.
00012 # 
00013 # If you call the module as a script, it will print the tree to stdout
00014 # for the directory you pass in on the command line (defaults to '.').
00015 # 
00016 # Copyright (C) 2002 GDS Software
00017 # 
00018 # This program is free software; you can redistribute it and/or
00019 # modify it under the terms of the GNU General Public License as
00020 # published by the Free Software Foundation; either version 2 of
00021 # the License, or (at your option) any later version.
00022 # 
00023 # This program is distributed in the hope that it will be useful,
00024 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00025 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026 # GNU General Public License for more details.
00027 # 
00028 # You should have received a copy of the GNU General Public
00029 # License along with this program; if not, write to the Free
00030 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00031 # MA  02111-1307  USA
00032 # 
00033 # See http://www.gnu.org/licenses/licenses.html for more details.
00034 # 
00035 
00036 __version__ = "$Id: tree.py,v 1.3 2002/08/21 12:41:49 donp Exp $"
00037 
00038 def visit(list, dirname, names):
00039     list.append(dirname)
00040 
00041 def Tree(dir, indent=4, leading_char=" "):
00042     import os, string, re
00043     list = []
00044     dir_list = []
00045     os.path.walk(dir, visit, list)
00046     list.sort()
00047     head = re.compile("^" + dir)
00048     indent_str = leading_char +  " " * (indent - 1)
00049     for directory in list: 
00050         if directory == ".":
00051             continue
00052         y = string.replace(directory, "\\", "/")
00053         y = head.sub("", y)
00054         fields = string.split(y, "/")
00055         count = len(fields) - 1
00056         if dir == "/":
00057             count = count + 1
00058         if fields[-1]:
00059             str = indent_str * count + fields[-1]
00060             dir_list.append(str)
00061     return [dir] + dir_list
00062 
00063 if __name__ == "__main__":
00064     import sys
00065     dir_to_process = "."
00066     if len(sys.argv) == 2:
00067         dir_to_process = sys.argv[1]
00068     leading_char = "|"
00069     for dir in Tree(dir_to_process, leading_char=leading_char):
00070         print dir
00071 
00072 
00073 

© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."

Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.

122,241 lines of code and growing...