文件目录tree显示,python

#/usr/bin/python
import os 
 
def travelTree(currentPath, count=0):
    if not os.path.exists(currentPath):
        print "no current Path"
        return 
    if os.path.isfile(currentPath):
        fileName = os.path.basename(currentPath)
        print "  "*count + "|_ "+fileName 
    elif os.path.isdir(currentPath):
    	#print currentPath 
        print "  "*count + "|_ "+currentPath 
    	pathList = os.listdir(currentPath)
        for eachPath in pathList:
            travelTree(currentPath + '/' + eachPath, count + 1)
    else :
        print "\n"
    	return

travelTree('/Users/liyi/Desktop')
#travelTree('/Users/liyi/Desktop/test.sh', 1)
posted @ 2017-02-05 10:28  clq.lib  阅读(496)  评论(0编辑  收藏  举报