zoukankan      html  css  js  c++  java
  • List contents of directories in a tree-like format

      Python programming practice.

      Usage: List contents of directories in a tree-like format.

    #!/usr/bin/python
    #Author: lxw0109
    #Date: 20140719
    #Usage: List contents of directories in a tree-like format.
    
    import os
    import sys
    
    
    def tree(directory, count):
        if os.path.isdir(directory):
            print((count + 1) * "|   " + "|---" + os.path.basename(directory))
            # Get the file/directory list in 'dir'
            dirFormat = os.listdir(directory)
            dirFormat.sort()
    
            for dirItem in dirFormat:
    
                #absPath = os.path.abspath(dirItem)    
                #NO: On most platforms, this is equivalent to calling the function normpath() as follows:
                #normpath(join(os.getcwd(), path))
    
                absPath = directory + os.sep + dirItem
                tree(absPath, count + 1)
        else:
            print((count + 1) * "|   "+ "|---" + os.path.basename(directory))
    
    
    def main():
        #print(sys.argv)    #NOTE: sys.argv is a list.
        if len(sys.argv) != 2:
            print("Usage: tree DirectoryName")
            sys.exit(0)
    
        #directory = "/home/lxw/Documents/Programing"
        directory = sys.argv[1]
    
        #Get rid of the '/' at the end.
        if directory.endswith(os.sep):
            directory = directory[:-1]
    
        #turn Relative Path / Absolute Path into Absolute Path.
        if directory[0] != '/':
            #print("RELATIVE: " + directory[0])
            directory = os.getcwd() + os.sep + directory
            #print("direcotry: " + directory)
    
        #count = directory.count(os.sep)
        tree(directory, -1)
    
    if __name__ == "__main__":
        main()

    Demo Output:

    lxw@ubuntu:~/Documents/Programing/Python$ ls
    BasicPython.py Funcclass.py QS.py tree
    Django netTree.py tranverDict.py tree~
    lxw@ubuntu:~/Documents/Programing/Python$ ./tree .
    |---.
    |   |---BasicPython.py
    |   |---Django
    |   |   |---myFirstSite
    |   |   |   |---db.sqlite3
    |   |   |   |---manage.py
    |   |   |   |---myFirstSite
    |   |   |   |   |---__init__.py
    |   |   |   |   |---__init__.pyc
    |   |   |   |   |---settings.py
    |   |   |   |   |---settings.pyc
    |   |   |   |   |---urls.py
    |   |   |   |   |---urls.pyc
    |   |   |   |   |---wsgi.py
    |   |   |   |   |---wsgi.pyc
    |   |---Funcclass.py
    |   |---QS.py
    |   |---netTree.py
    |   |---tranverDict.py
    |   |---tree
    |   |---tree~
    lxw@ubuntu:~/Documents/Programing/Python$
  • 相关阅读:
    关于AutoResetEvent和ManualResetEvent
    (转)使用 ODP.NET 和引用游标优化结果集
    胰腺
    SQL Cache Dependency
    败犬的远吠?
    吃亏和付出经常是必须的(转)
    AutoResetEvent 与 ManualResetEvent送花例子
    linux yum install
    SpringMVC+JPA+SpringData配置
    Spring AOP 实现原理
  • 原文地址:https://www.cnblogs.com/lxw0109/p/tree-like-format.html
Copyright © 2011-2022 走看看