2tfwe 发表于 2015-7-23 08:53:20

python实现目录递归遍历

#实现目录递归遍历,查找.log文件,并将结果存入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
res=[]
def findfile(dir):
    for i in os.listdir(dir):
      if os.path.isdir(dir+os.sep+i)==True:
            findfile(dir+os.sep+i)
      else:
            if i[-4:]=='.log':
                res.append(dir+os.sep+i)
findfile(os.path.abspath('.'))
#findfile('D:\Python\code')
#print len(res)

fp=open('result.log','w')
for i in res:
    fp.write(i+'\n')
fp.close()



页: [1]
查看完整版本: python实现目录递归遍历