第一种方式
import os dir = '/home/donghuixiedh/workspace' def readfile(dir): for f in os.listdir(dir): file = os.path.join(dir, f) if os.path.isdir(file): readfile(file) elif os.path.isfile(file): print 'file is ' + file readfile(dir) print '----------------- all dir'
def readfile2(dir): yid = os.walk(dir) for rootDir, pathList, fileList in yid: for file in fileList: print 'file ' + os.path.join(rootDir, file) for path in pathList: print 'path ' + os.path.join(rootDir, path)
转自:http://www.yabogo.com/?p=578