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$

List contents of directories in a tree-like format的更多相关文章

  1. Files and Directories

    Files and Directories Introduction     In the previous chapter we coveredthe basic functions that pe ...

  2. tree指令

    tree 中文解释:tree功能说明:以树状图列出目录的内容.语 法:tree [-aACdDfFgilnNpqstux][-I <范本样式>][-P <范本样式>][目录.. ...

  3. tree - 列出树状目录结构

    tree - list contents of directories in a tree-like format. 树状显示目录结构 常用格式: tree [option] [directory] ...

  4. 【Linux常见命令】tree命令

    tree - list contents of directories in a tree-like format. tree命令用于以树状图列出目录的内容. 执行tree指令,它会列出指定目录下的所 ...

  5. 编译安装tree命令

    查看当前的tree [12:33:33 root@C8[ ~]#rpm -qi tree Name : tree Version : 1.7.0 Release : 15.el8 Architectu ...

  6. Device Tree Usage( DTS文件语法)

    http://elinux.org/Device_Tree_Usage Device Tree Usage     Top Device Tree page This page walks throu ...

  7. A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python)

    A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python) MACHINE LEARNING PYTHON  ...

  8. Device Tree Usage 【转】

    转自:http://blog.chinaunix.net/uid-20522771-id-3457184.html 原文链接:http://devicetree.org/Device_Tree_Usa ...

  9. tree:以树形结构显示目录下的内容

    tree命令 1.命令详解 [功能说明] tree命令的中文意思为“树”,功能是以树形结构列出指定目录下的所有内容包括所有文件.子目录及子目录里的目录和文件. [语法格式] tree [option] ...

随机推荐

  1. ddr3调试经验分享(一)——modelsim实现对vivado中的MIG ddr3的仿真

    Vivado中的MIG已经集成了modelsim仿真环境,是不是所有IP 都有这个福利呢,不知道哦,没空去验证. 第一步:使用vivado中的MIG IP生成一堆东西 ,这个过程自己百度.或者是ug5 ...

  2. C#取调用堆栈StackTrace

    Environment.StackTrace or System.Diagnostics.StackTrace if you need a more convienient (i.e. not str ...

  3. linux ls命令按时间显示文件

      本文介绍下,使用ls命令显示文件,并按时间排序的方法,供大家学习参考. 在linux系统中,使用ls命令按时间排序文件,其实很简单,如下: #ls -tr 即可按时间排序当前目录下的文件. 附,l ...

  4. Razor 3、MVC 5

    Razor 3 需要vs 2012 update 4 才可以 需要装一个 Microsoft ASP.NET and Web Tools 2013.1 才会有 MVC 5

  5. mysql的binlog和slow_log慢日志

    redo undo 锁 ----------------------------------------- 日志管理 log-error=/var/log/mysql.log 二进制日志的“总闸” 作 ...

  6. SecurCRT 远程linux 输入中文及 oracle 查询出文号问题

    一. 首先确认你的linux是否设置了支持中文 cat /etc/sysconfig/i18n 其中: LANG 变量是 language 的简称, 这个变量时决定系统的默认语言, 即系统菜单, 程序 ...

  7. php对gzip文件或者字符串解压实例参考

    要采集一个网站,目标站采用了gzip压缩传输网页,本来应该只要发送一个http头 Accept-Encoding: identity或者干脆不发送这个头等,就可以使目标站返回没有经过gzip压缩的页面 ...

  8. 我的第三个java程序 两数相加

    import java.util.Scanner; public class Test { public static void main(String [] args) { Scanner sc = ...

  9. Matlab命令行版打开

    matlab -nosplash -nodesktop 运行文件:matlab  -nodesktop -nosplash -r file

  10. Spring MVC属性方法名称解析器

    以下示例显示如何使用Spring Web MVC框架来实现多动作控制器的属性方法名称解析器. MultiActionController类可在单个控制器中分别映射多个URL到对应的方法. 所下所示配置 ...