import xmltodict import json from pathlib import Path basepath= Path('DRAMSys/library/resources/configs/mcconfigs') for path in basepath.iterdir(): if path.is_file(): filename=str(path) if filename.endswith('.xml'): # if os.stat(path).st_size != 0: #check if it is empty with open(path, "r") as in_file: xml = in_file.read() with open(filename.replace(".xml",".json"), 'w') as out_file: json.dump(xmltodict.parse(xml), out_file)