Python: How to execute a function on an X number of files (.XML)?

0

I need to run a Python function on a large number of .XML files. I need the parsing function to run on all the files that end in .XML.

This is a fragment of the code to parse the .XML file:

import os 
import xml.etree.ElementTree as ET



dirpath = os.getcwd()
file_list = []

for root,_, filenames in os.walk(dirpath):

    for filename in filenames:
        file_list.append(os.path.join(root, filename))

for filename in file_list:

    if filename.endswith('.XML'):

Up to this point it shows all the files that end in .XML, but I can not decipher what the code would be so that the function runs in all those files.

def printXML():

            #Se declaran variables para ubicar los elementos requeridos
            file = ET.parse() #Parsea el documento
            root = pnr.getroot() #Se ubica en la raiz del documento
            loc = root.findall('root') #Busca todos los elementos(childs) de root
            oficina = root.findall('root/child1') #Busca todos los elementos de child1


            #Buscar todos los archivos que terminan en XML


            #A partir de este punto se empiezan a imprimir toda la informacion utilizando for loop


            for lo in loc:
                print 'Localizador:', lo.find(child12).text
                print 'Version:', lo.find(child12).text


            for og in oficina:
                print 'token:', og.find('child21').text
                print 'office', og.find('child22').text
                print 'description', og.find('child23').text

printXML()

In this case it only shows me in the console the file that I extracted from the .XML ... I need that that it printed to realize it to all the XML files that are in X folder ...

The code that I previously placed already places the files, but I do not know how to start or where to look or how to start the loop so that it executes this function to all the .XML files

    
asked by mega_yizuz 02.06.2017 в 15:12
source

1 answer

0

From what I understand, your routine should be rewritten like this:

def printXML(file):

            #Se declaran variables para ubicar los elementos requeridos
            tree = ET.parse(file) #Parsea el documento
            root = tree .getroot() #Se ubica en la raiz del documento
            loc = root.findall('root') #Busca todos los elementos(childs) de root
            oficina = root.findall('root/child1') #Busca todos los elementos de child1

            for lo in loc:
                print 'Localizador:', lo.find(child12).text
                print 'Version:', lo.find(child12).text

            for og in oficina:
                print 'token:', og.find('child21').text
                print 'office', og.find('child22').text
                print 'description', og.find('child23').text

And as you said @FJSevilla, the process of each file would do it like this:

for filename in file_list:
    if filename.endswith('.XML'):
       printXML(filename)
    
answered by 29.06.2017 в 22:04