使用Python在指定目录搜索指定文件名

2014-11-13 0 634
使用Python在指定目录搜索指定文件名
# -*- coding: utf-8 -*-
'''
Created on 2014年12月1日

@author: zhrb
'''


import os

count = 0



    
def search(dstDir,fileName):
    global count
    #files = [os.path.join(dstDir,x) for x in os.listdir(dstDir)]
    for y in os.listdir(dstDir):
        absPath = os.path.join(dstDir,y)
        if os.path.isdir(absPath):
            try:
                search(absPath,fileName)
            except BaseException, e:
                continue
            
        elif (os.path.isfile(absPath) and os.path.split(absPath)[1].lower()==fileName.lower()):
            count +=1
            print('found %s '%absPath.decode('gbk').encode('utf-8'))

search('d:\\','1.jpg')
print("%d founded"%count)

遇见资源网 python 使用Python在指定目录搜索指定文件名 http://www.ox520.com/14867.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务