1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
import os
allFileNum = 0 def printPath(level, path): global allFileNum ''' 打印一个目录下的所有文件夹和文件 ''' dirList = [] fileList = [] files = os.listdir(path) dirList.append(str(level)) for f in files: if(os.path.isdir(path + '/' + f)): if(f[0] == "."): pass else: dirList.append(f) if(os.path.isfile(path + '/' + f)): fileList.append(f)
i_dl = 0 for dl in dirList: if (i_dl == 0): i_dl += 1 else: print '-' * (int(dirList[0])), dl printPath((int(dirList[0]) + 1), path + '/' + dl) for fl in fileList: print '-' * (int(dirList[0])), fl allFileNum = allFileNum + 1
if __name__ == '__main__': printPath(1, 'D:\python\python_tool') print "总文件数 = ", allFileNum
|