用Python在指定目录搜索需要的文件名,python文件名,用Python在指定目录


用Python在指定目录搜索需要的文件名(忽略大小写)
仅适用于英文文件名
主要用于学习python

1.[用Python在指定目录搜索需要的文件名代码][Python]代码

# -*- coding: utf-8 -*-'''@author: zhrb'''import oscount = 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)

编橙之家文章,

评论关闭