Skip to content Skip to sidebar Skip to footer

Parse All The Xml Files In A Directory One By One Using Elementtree

I'm parsing XML in python by ElementTree import xml.etree.ElementTree as ET tree = ET.parse('try.xml') root = tree.getroot() I wish to parse all the 'xml' files in a given direct

Solution 1:

Just create a loop over os.listdir():

import xml.etree.ElementTree as ET
import os

path = '/path/to/directory'
for filename in os.listdir(path):
    if not filename.endswith('.xml'): continue
    fullname = os.path.join(path, filename)
    tree = ET.parse(fullname)

Post a Comment for "Parse All The Xml Files In A Directory One By One Using Elementtree"