Ammar Ahmad
Ammar Ahmad

Reputation: 41

unable to print the print into textfile from another textfile in python

Attempting to use a code to pull all .xml file names from a particular directory in a repo, then fliter out all the files which contain a certain key word. The part of the code which pulls the file names and puts it into the first text file works properly. The second part which takes the first text file and filters it into a second text file does not work as anticipated. I am not recieving an error or anything but the array of lines is empty which is odd because the text file is not. I am wondering if anyone sees anything obvious that I am missing I have been at this for a long time so its easy to miss simple things any help would be appreciated. I have looked into other examples and the way they did it is similar to mine logically just not sure where I went wrong. Thank you in advance

This is the code:

#!/usr/bin/python

import glob
import re 
import os
import fnmatch
from pprint import pprint
import xml.etree.ElementTree as ET
import cPickle as pickle


#open a text output file
text_file = open("TestCases.txt", "w")
matches = []
#initialize the array called matches with all the xml files in the selected directories (bbap, bbsc, bbtest, and bbrtm

for bbdir in ['bbap/nr', 'bbsc/nr','bbtest/nr', 'bbrtm/nr']:
    for root, dirnames,filenames in os.walk('/repo/bob/ebb/'+bbdir):
        for filename in fnmatch.filter(filenames, '*.xml'):
            matches.append(os.path.join(root,filename))

#for each listing in matches test it against the desired filter to achieve the wanted tests 
for each_xml in matches:
    if each_xml.find('dlL1NrMdbfUPe') != -1:
        tree = ET.parse(each_xml)
        root = tree.getroot()
        for child in root: 
            for test_suite in child:
                for test_case in test_suite:
                    text_file.write(pickle.dumps(test_case.attrib))         

#modify the text so it is easy to read
with open("TestCases.txt") as f:
    with open("Formatted_File", "w") as f1:
        for line in f:
            if "DLL1NRMDBFUPE" in line:
                f1.write(line)

Upvotes: 0

Views: 48

Answers (1)

Ammar Ahmad
Ammar Ahmad

Reputation: 41

Just As I had anticipated, the error was one made from looking at code for too long. I simply forgot to close text_file before opening f.

Fix:

import glob
import re 
import os
import fnmatch
from pprint import pprint
import xml.etree.ElementTree as ET
import cPickle as pickle


#open a text output file
text_file = open("TestCases.txt", "w")
matches = []
#initialize the array called matches with all the xml files in the selected directories (bbap, bbsc, bbtest, and bbrtm

for bbdir in ['bbap/nr', 'bbsc/nr','bbtest/nr', 'bbrtm/nr']:
    for root, dirnames,filenames in os.walk('/repo/bob/ebb/'+bbdir):
        for filename in fnmatch.filter(filenames, '*.xml'):
            matches.append(os.path.join(root,filename))

#for each listing in matches test it against the desired filter to achieve the wanted tests 
for each_xml in matches:
    if each_xml.find('dlL1NrMdbfUPe') != -1:
        tree = ET.parse(each_xml)
        root = tree.getroot()
        for child in root: 
            for test_suite in child:
                for test_case in test_suite:
                    text_file.write(pickle.dumps(test_case.attrib))         
**text_file.close()**

#modify the text so it is easy to read
with open("TestCases.txt") as f:
    with open("Formatted_File", "w") as f1:
        for line in f:
            if "DLL1NRMDBFUPE" in line:
                f1.write(line)
f.close()
f1.close()

Upvotes: 0

Related Questions