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. C# 运行时中的泛型

    将泛型类型或方法编译为 Microsoft 中间语言 (MSIL) 时,它包含将其标识为具有类型参数的元数据. 泛型类型的 MSIL 的使用因所提供的类型参数是值类型还是引用类型而不同. 第一次用值类 ...

  2. OpenCv中基本数据类型--Point,Size,Rect,Scalar,Vec3b类类型的详细解释

    头文件路径:opencv-2.4.9/modules/core/include/opencv2/core/core.hpp 一.Point类 在这些数据类型中,最简单的就是Point点类,Point类 ...

  3. iOS开发之多文件上传

    // //  ViewController.m //  B03-多文件上传 // //  Created by 0426iOS on 15/7/1. //  Copyright (c) 2015年 0 ...

  4. ini_set ini_get 可操作配置参数列表

    为了使自己的程序在不同的平台中拥有更好的兼容性,很多时候我们都要获取当前Php的运行环境参数. 比如我们常用到的: 获取 magic_quotes_gpc 状态,来决定当表单提交时我们是否转义(add ...

  5. Linux vi 文件编辑

    1.通过vi filename 进入编辑状态 2.退出 vi 编辑器 退出命令 说明 q 如果文件未被修改,会直接退回到Shell:否则提示保存文件. q! 强行退出,不保存修改内容. wq w 命令 ...

  6. AndroidManifest.xml详细分析

    原文地址: http://my.eoe.cn/1087692/archive/5927.html 一.关于AndroidManifest.xmlAndroidManifest.xml 是每个andro ...

  7. mysqldump: Got error: 1045

    问题:最近备份mysql然后在执行mysqldump的时候提示权限和密码有问题 报错: Warning: Using a password on the command line interface ...

  8. Android开发:《Gradle Recipes for Android》阅读笔记1.5

    这节讲的是如何如何添加JAVA依赖库. 默认的android项目有两个build.gradle文件,分别位于顶级目录,和应用自己的目录下(通常放在一个叫app的目录下面). gradle支持多种方式列 ...

  9. [Docker]学习笔记--简单介绍

    学习docker已经有一段时间了,一直没有静下心来好好总结一下. 最近用docker搭了一整套Gitlab的持续集成环境.(会在下一篇中详细的讲解具体步骤,敬请期待) 感觉是时候写点东西和大家一起分享 ...

  10. 【BZOJ4917】Hash Killer IV 乱搞

    [BZOJ4917]Hash Killer IV Description 有一天,tangjz造了一个Hash函数: unsigned int Hash(unsigned int v){     un ...