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$
List contents of directories in a tree-like format的更多相关文章
- Files and Directories
Files and Directories Introduction In the previous chapter we coveredthe basic functions that pe ...
- tree指令
tree 中文解释:tree功能说明:以树状图列出目录的内容.语 法:tree [-aACdDfFgilnNpqstux][-I <范本样式>][-P <范本样式>][目录.. ...
- tree - 列出树状目录结构
tree - list contents of directories in a tree-like format. 树状显示目录结构 常用格式: tree [option] [directory] ...
- 【Linux常见命令】tree命令
tree - list contents of directories in a tree-like format. tree命令用于以树状图列出目录的内容. 执行tree指令,它会列出指定目录下的所 ...
- 编译安装tree命令
查看当前的tree [12:33:33 root@C8[ ~]#rpm -qi tree Name : tree Version : 1.7.0 Release : 15.el8 Architectu ...
- Device Tree Usage( DTS文件语法)
http://elinux.org/Device_Tree_Usage Device Tree Usage Top Device Tree page This page walks throu ...
- 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 ...
- Device Tree Usage 【转】
转自:http://blog.chinaunix.net/uid-20522771-id-3457184.html 原文链接:http://devicetree.org/Device_Tree_Usa ...
- tree:以树形结构显示目录下的内容
tree命令 1.命令详解 [功能说明] tree命令的中文意思为“树”,功能是以树形结构列出指定目录下的所有内容包括所有文件.子目录及子目录里的目录和文件. [语法格式] tree [option] ...
随机推荐
- 用VMware 8安装Ubuntu 12.04具体过程(图解)
图解演示环境版本号: 本机系统: WIN7 虚拟机:VMware Workstation 8 (英文版) 安装目标:Ubuntu Desktop 12.04 LTS (请点击这里)先下载好iso镜像 ...
- springMVC集成mybatis-paginator实现分页
mybatis-paginator下载地址:https://github.com/miemiedev/mybatis-paginator 1.引入maven依赖 <dependency> ...
- 自动make工具--autotools
自动生成Makefile GNU提供的autoconf和automake两套工具可自动完成符合自由软件惯例的makefile的编写.这样就可以像常见的GNU程序一样,只要使用“./configure” ...
- hdu2068 RPG的错排 错排+组合
RPG的错排 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 修改Nginx与Apache上传文件大小限制
一.修改Nginx上传文件大小限制 我们使用ngnix做web server的时候,nginx对上传文件的大小有限制. 当超过大小的时候会报413错误. 这个时候我们要修改nginx参数. sudo ...
- Eclipse 创建 Java 接口
打开新建 Java 接口向导 新建 Java 接口向导可以创建新的 Java 接口.打开向导的方式有: 点击 File 菜单并选择 New > Interface 在 Package Explo ...
- tinycore Network card configuration during exec bootlocal.sh
question: tinycore在boot时, 运行bootlocal.sh脚本,其中有局域网通信的部分,一直跑不通,测试了一下才知道是运行bootlocal.sh的阶段,网络可能没有配置好,ip ...
- Ubuntu下MongoDB的安装和使用
本博文介绍了MongoDB,并详细指引读者在Ubuntu下MongoDB的安装和使用.本教程在Ubuntu14.04下测试通过.(2017.09.07) 安装MongoDB MongoDB安装很简单, ...
- Android 定时器Timer的使用
定时器有什么用 在我们Android客户端上有时候可能有些任务不是当时就执行,而是过了一个规定的时间在执行此次任务.那么这个时候定时器的作用就非常有用了.首先开启一个简单的定时器 Timer time ...
- Android-NDK编译
(2013-12-19 21:48:21 其实一切还是先看看官网的好,乱百度浪费时间.... http://developer.android.com/tools/sdk/ndk/index.htm ...