2

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
Alice Palmer
  • 121
  • 2

4 Answers4

0
my_files = os.listdir("/all_files/") #put your input directory here
my_writer = open(r'C:\temp\20160309_test_xml\out.txt', 'w')
for fn in my_files:
    if not fn.endswith(".xml"):
         continue
    with open(fn, 'r') as f: #open and read from every xml file
         for line in f:
             if "OBJNAM" in line or "srfres" in line:
                  my_writer.write(line)
my_writer.close()
Garrett R
  • 2,687
  • 11
  • 15
0

Or maybe you can try with list comprehension

[(name,lines) for name,lines in enumerate(xml_list)]
Richard Rublev
  • 7,718
  • 16
  • 77
  • 121
0

It looks like you're not opening files in your loop and instead you're iterating over paths.

I have few suggestions regarding your code:

You can iterate over file lines like that:

with open(path, 'r') as file:
    for line in file:
        print line

Also, when dealing with xml files, maybe you could use xml lib.

pgrzesik
  • 1,869
  • 13
  • 14
0
import glob
import os

xmlDir = r'C:\temp\test_xml'
xmlFiles = glob.glob(os.path.join(xmlDir, "*.xml"))
outfilepath = r'C:\temp\20160309_test_xml\out.txt'
keywords = ['OBJNAM', 'srfres']

copy = False
with open(outfilepath, 'w') as outfile:
    for fpath in xmlFiles:
        with open(fpath) as infile:
            for line in infile:
                if copy: outfile.write(line)

                if any (w in line for w in keywords):
                    copy = True
                    continue
                else:
                    copy = False
                    continue
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241