Python script to count the number of different resource types in a set of project files

Just in case anyone else finds this of use! This Python script counts the number of different resource types in a set of project files:

from os import listdir
import pprint
import xml.etree.ElementTree as ET
folder = '/base/folder/here'
resource_dict = {}

for project_file in listdir(folder):
	elements = ET.parse('{}/{}'.format(folder, project_file)).getroot().find('resources').findall('resource')
	for element in elements:
		resource_type = element.attrib['type']
		if resource_type in resource_dict:
			resource_dict[resource_type] += 1
		else:
			resource_dict[resource_type] = 1
pp = pprint.PrettyPrinter(indent = 2)
pp.pprint(resource_dict)
2 Likes