# -*- 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)