python迭代查找目录下文件

时间:2015-05-14 20:48:06   收藏:0   阅读:205
#两种方法
#!/usr/bin/env python
import os

dir=‘/root/sh‘
‘‘‘
def fr(dir):
  filelist=os.listdir(dir)
  for i in filelist:
    fullfile=os.path.join(dir,i)
    if not os.path.isdir(fullfile):
      if i == "1.txt":
        #print fullfile
	os.remove(fullfile)
    else:
      fr(fullfile)

‘‘‘

‘‘‘
def fw()dir:
  for root,dirs,files in os.walk(dir):
    for f in files:
      if f == "1.txt":
        #os.remove(os.path.join(root,f))
        print os.path.join(root,f)
‘‘‘


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!