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] ...
随机推荐
- Atitit.跨语言数据库db api兼容性 jdbc odbc ado oledb 增强方案
Atitit.跨语言数据库db api兼容性 jdbc odbc ado oledb 增强方案 1. 跨语言db api兼容性..1 2. 目前访问数据库的接口很多.比较常用的jdbc odbc 以 ...
- Django的模型
Django的模型类相当于数据库的一张表,模型类的实例对象相当于表中的一行内容 Django提供了多种创建对象的方式,常用如下: 通过create()方法 1.Person.objects.creat ...
- Solve Equation gcd(x,y)=gcd(x+y,lcm(x,y)) gcd(x,y)=1 => gcd(x*y,x+y)=1
/** 题目:Solve Equation 链接:http://acm.hnust.edu.cn/JudgeOnline/problem.php?id=1643 //最终来源neu oj 2014新生 ...
- 21:开发脚本管理服务端LVS案例
[root@lb03 scripts]# cat lv_manager.sh #!/bin/bash #定义只能是root用户执行 ];then echo "permission deny ...
- OpenFiler configuration
5.OpenFiler configuration 登录Openfiler 步骤1:安装 FireFox 步骤2:通过FireFox以HTTPS://server-ip:446 远程访问O ...
- Linux 总结2
cd pwd mkdir ...
- TP view中跳转到某个控制器
#直接用 __MODULE__/控制器名/方法名/参数名/参数 <a href="__MODULE__/Product/list_table/goods_id/<?php ech ...
- Matlab命令行版打开
matlab -nosplash -nodesktop 运行文件:matlab -nodesktop -nosplash -r file
- [PHP开发必备] -- 小巧强悍的MYSQL-Front中文版使用教程,附最新版下载地址
PHP开发已经各位工程师轻车熟路的事情,MySQL也是经常都要用的产品,能有一个简单而又强大的管理工具,那可是咱们PHP开发工程师的福气. 下面浪风小园子就为大家介绍下来自国外小巧的MySQL管理工具 ...
- 用于ARM上的FFT与IFFT源代码-C语言
/********************************************************************************* 程序名称:快速傅里叶变换(FFT) ...