I am trying to create a loop to go through ~3,000 .xml files to search for certain lines and print out into one txt file.
My original code which i am able to use on one xml is:
my_file = open(r'C:\temp\20160309_test_xml\out.txt', 'w')
with open(r'C:\temp\20160309_test_xml\test.xml', 'r') as file:
for name, lines in enumerate(file, 1):
if "OBJNAM" in lines:
my_file.write(file.next())
with open(r'C:\temp\20160309_test_xml\test.xml', 'r') as file:
for number, line in enumerate(file, 1):
if "srfres" in line:
my_file.write(file.next())
file.close()
I've attempted to create a loop for this but the output txt is printing blank:
import glob
import os
path = r'C:\temp\test_xml'
xml_directory = os.path.join(path, '*.xml')
xml_list = glob.glob(xml_directory)
my_file = open(r'C:\temp\20160309_test_xml\out.txt', 'w')
for xml in xml_list:
for name, lines in enumerate(xml, 1):
if "OBJNAM" in lines:
my_file.write(file.next())
for xml in xml_list:
for number, line in enumerate(xml, 1):
if "srfres" in line:
my_file.write(xml.next())
my_file.close()
print