多重文件路径下遍历统计后缀为png和xml的个数

# -*- coding: utf-8 -*-
"""
Created on Fri Sep 28 15:45:44 2018

@author: mailb
"""


import os


path = "./test1/"                   # 文件路径

global count_png,count_xml


def read_fileLists(path):                             #多层路径
   
    #获得当前路径下所有文件
    dirList = os.listdir(path)
    #print(dirList)
    
    for f in dirList:
        #获得目录下文件的绝对路径
        #print(f)
        file = os.path.join(path,f)
        
        #判断该文件是否是目录类型
        if   not(os.path.isdir(file)):
            
            filepath = os.path.dirname(file)             #file 的上一级目录
            print(filepath)
            get_png_xml(filepath)
            break
        
        else:
            # 进行递归,继续寻找该当前文件夹的子文件
            read_fileLists(file)
        
        
            
            
        
    
def get_png_xml(path):                                 #一层
    
    global count_png,count_xml
    count_png = 0
    count_xml = 0
    
    dirlist = os.listdir(path)
    for file in dirlist:
        if file.endswith('png'):
           count_png = count_png +1
           
        if file.endswith('xml'):
            count_xml = count_xml + 1
            
    print("count_png:%s"%count_png)
    print("count_xml:%s"%count_xml)
    #解决上面不能输出最后一个文件夹的统计个数            
          

if __name__ == '__main__':
    filepath = read_fileLists(path)
    
    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值