import os, os.path,sys from xml.etree import ElementTree as et def select_files_in_folder(dir, ext): for file in os.listdir(dir): if file.endswith('.%s' % ext): if file.startswith(sys.argv[2]): continue yield os.path.join(dir, file) def combine_xml(files): first = None for filename in files: data = et.parse(filename).getroot() if first is None: first = data else: first.extend(data) if first is not None: return et.ElementTree(first).write(sys.argv[2]) def run(files): content = None fileNum = 0 content = combine_xml(files) if content is not None: with open(sys.argv[2], "w") as text_file: text_file.write(str(content)) if __name__ == "__main__": if (len(sys.argv) != 3): print(""" USAGE: XMLmerrge is the directory, where the source xml files are is the path and name of the merged xml file. Example XMLmerrge trafficSrc trafficSrc\trafficSrc.xml """) sys.exit(1) else: print ("Start XML merging in '{dir}' folder ...".format(dir=sys.argv[1])) fileNum = run(select_files_in_folder(sys.argv[1], 'xml')) print ("Done")