title: How convert multiple xml files in a folder in csv files with python?
body: <p>1-I ready have a code than convert de xml file to csv. But actually I need to do this operation a lot times and then a need to change the name of the xml file in my program each time.I want to do a loop to read each '.xml' file in the folder location and do for all file. there is my original code</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  import csv
  
  tree = ET.parse("Shot_30AA.xml")
  root = tree.getroot()
  
  Shot_30AA = open('Shot_30AA.csv', 'w', newline='')
  csvwriter = csv.writer(Shot_30AA)
  head = []
  
  ShotCode = root.attrib['ShotCode']
  csvwriter.writerow(['ShotCode', ShotCode])
  head.append(ShotCode)
  
  for member in root.getchildren():
    submembers = member.getchildren()
    keys = submembers[0].attrib.keys()
    csvwriter.writerow ("\n")
    csvwriter.writerow(keys)
    for submember in submembers:
      row_data = [submember.attrib[k] for k in keys]
      csvwriter.writerow(row_data )
  Shot_30AA.close()
  </code></pre>
  
  <p>I tried to do this operation in my file folder adding the fallowing code to do only one time </p>
  
  <pre><code>path = 'C:/Users/Desktop/Program'
  for filename in os.listdir(path):
     if not filename.endswith('.xml'): continue
       ShotFile = os.path.join(path, filename)
       tree = ET.parse(ShotFile)
       root = tree.getroot()
  
       filename = open( filename'.csv', 'w', newline='')
       csvwriter = csv.writer(filename)
       head = [] 
  
       ShotFile = open('ShotFile.csv', 'w', newline='')
       csvwriter = csv.writer(Shot_30AA)
       head = []
  
       ShotCode = root.attrib['ShotCode']
       csvwriter.writerow(['ShotCode', ShotCode])
       head.append(ShotCode)
  
       for member in root.getchildren():
         submembers = member.getchildren()
         keys = submembers[0].attrib.keys()
         csvwriter.writerow ("\n")
         csvwriter.writerow(keys)
         for submember in submembers:
           row_data = [submember.attrib[k] for k in keys]
           csvwriter.writerow(row_data )
    ShotFile.close()
  </code></pre>
  
  <p>I expect to obtain all my files in csv format with one run.</p>