话不多说上代码(新手学习ing):
#!/usr/bin/env python # -*- coding: utf-8 -*- import os def listDir1(rootDir): list_dirs = os.walk(rootDir) for root, dirs, files in list_dirs: for d in dirs: print os.path.join(root, d) for f in files: print os.path.join(root, f) def listDir2(rootDir): for lists in os.listdir(rootDir): path = os.path.join(rootDir, lists) print path if os.path.isdir(path): listDir2(path) def listDir3(rootDir, level=1): if level==1: print rootDir for lists in os.listdir(rootDir): path = os.path.join(rootDir, lists) print '| '*(level-1)+'|--'+lists if os.path.isdir(path): listDir3(path, level+1) if __name__ == '__main__': listDir1('C:php-5.2.17-Win32-VC6-x86') print listDir2('C:php-5.2.17-Win32-VC6-x86') print listDir3('C:php-5.2.17-Win32-VC6-x86')
效果可以分别去尝试一下,第二、三种进行了简单的排序。