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. nginx的luajit安装luarocks并安装luafilesystem

    nginx的luajit安装luarocks并安装luafilesystem by admin on -- :: in , 69次 标题有点绕口.我尽量把关键词都贴进去.之前因为自己的nginx安装了 ...

  2. poj 3666 河南省第七届程序设计D题(山区修路)

    题目大意: 给定一个序列,以最小代价将其变成单调不增或单调不减序列,求最小的变动价值:需要用到离散化dp 状态转移方程: dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k ...

  3. 2017-5-14 湘潭市赛 Highway 先获得直径S,T。则一开始S,T相连,然后其他的点如果离S更远那么连在S,否则T;

    Highway Accepted : Submit : Time Limit : MS Memory Limit : KB Highway In ICPCCamp there were n towns ...

  4. Tensorflow如何选择GPU

    CUDA_VISIBLE_DEVICES=1 python run.py or import os os.environ["CUDA_VISIBLE_DEVICES"]=" ...

  5. ssh 面试

    Struts1工作原理1. 初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总 控制器会读取配置文件(st ...

  6. Android实现短信监听并且转发到指定的手机号,转发后不留痕

    转载:http://blog.csdn.net/swqqcs/article/details/7252419 通过这些代码也可以对远程手机实现短信控制.有兴趣的可以自己改一下,说一下简单的原理,要实现 ...

  7. PostgreSQL tips

    tip 1 在sql中我们可以设置一个列自增长identity(1,1),但在postgresql中却没有这个关键字定义.但postgresql也有实现相关功能,那就是只需要将该列数据类型标记为ser ...

  8. lumen-Permission 权限管理使用心得

    安装 composer require spatie/laravel-permission github上有详细介绍:https://github.com/spatie/laravel-permiss ...

  9. 在程序内部使用winGraphviz进行图形自动布局

    winGraphviz支持內部圖形形狀進行佈局圖輸出.當然,在我們程序內部並不需要這樣的一個圖,因為我們的圖可能需要其它的繪製元素,而且我們還會在圖形上進行拖動.放大.縮小等功能,一張簡單的佈局圖是不 ...

  10. [转]Linux进程通信之POSIX消息队列

    进程间的消息队列可以用这个实现,学习了下. http://blog.csdn.net/anonymalias/article/details/9799645?utm_source=tuicool&am ...