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] ...
随机推荐
- 如何高效利用github提升自己
作为开源代码库以及版本控制系统,Github拥有超过900万开发者用户,是开发者打开程序开源大门的一扇窗口,也是开发者快速提升自己的一个重要途径.本文将从两个方面介绍github的使用方式. 和逛微博 ...
- Django学习之项目结构优化
其实就是采用包结构,比如: 目录models,包含__init__.py,a.py,b.py 然后将model class写在a和b中,但是这样的话,导入时就要改变了! from models imp ...
- random实现验证码功能
直接上代码: #-*- coding: utf-8 -*- #一个简单的验证码程序 import random #定义一个全局变量,初始值为空字符串 checkcode = '' for i in r ...
- stm32开发板无法正常写入的问题或者写入后无法正常运行的问题
在调试stm32的程序的时候遇到这样一个问题. 用Keil往stm32里写入程序时出现无法访问内存的错误.情况如下 然后 J-link没有提示异常.偶尔可以下载成功,但是程序无法正确的执行. 经过一番 ...
- 【ASP.NET】——AdRotator控件
AdRotator控件即广告控件. 广告,是站点不可缺少的一部分.也是站点获取收益的最主要途径,但最初制作广告非常麻烦.asp.net就将该方法封装成了一个控件.为我们节省了非常多时间.这也是ASP. ...
- linux USR1亦通常被用来告知应用程序重载配置文件
linux kill 命令 以及 USR1 信号 解释 原创 2016年03月16日 16:48:27 标签:linux kill -USR1 5325 最近 在做 服务器搭建的一些工作,其中 用到了 ...
- 记录下push推送优化改进点
一)自主研发的push服务的特点及优势: 1) 消息回执确认(ack); 2) 有效期推送(设置消息的有效期); 3) 精准推送(设置设备组别推送); 4) 下发任务分解(拆分任务,多进程); 5) ...
- 基于 UML 的业务建模举例
简介: 对于管理流程咨询项目.大型信息化建设项目和套装管理软件实施项目,对业务环境的分析和理解对项目的成功至关重要.系统.全面理解 IT 系统所处的业务环境,可以帮助 IT 系统能提供正确系统功能,并 ...
- (转)Resources和AssetBundle(新旧版)学习
Resources: Resources的缺点:1.与显示Inspector上直接引用相比,Resources使用不方便. 2.不管你Resources上的资源是否调用了,当你发布的时候, ...
- CStdioFile UNICODE编译 读取中文汉字乱码 .
函数原形为:char *setlocale( int category, const char *locale );头文件:<locale.h>所支持的操作系统为:ANSI, Win 95 ...