1

I keep getting the followinge error whenever there is a function call to xml(productline), but if I replace the functioncall with file = open('config\\' + productLine + '.xml','r'), it seems to work, why?

def xml(productLine):
         with open('config\\' + productLine + '.xml','r') as f:
    return f.read()


def getsanityresults(productline):

xmlfile=xml(productline) // replace with file = open('config\\' + productLine + '.xml','r')
dom = minidom.parse(xmlfile)
data=dom.getElementsByTagName('Sanity_Results')
#print "DATA"
#print data
textnode = data[0].childNodes[0]
testresults=textnode.data
#print testresults
for line in testresults.splitlines():
    #print line
    line = line.strip('\r,\n')
    #print line
    line = re.sub(r'(http://[^\s]+|//[^\s]+|\\\\[^\s]+)', r'<a href="\1">\1</a>', line)
    print line
    #print line       
    resultslis.append(line)
print resultslis
return resultslis

Error:

Traceback (most recent call last):
 File "C:\Dropbox\scripts\announce_build_wcn\wcnbuild_release.py", line 910, in <module>
  main()
 File "C:\Dropbox\scripts\announce_build_wcn\wcnbuild_release.py", line 858, in main
testresults=getsanityresults(pL)
 File "C:\Dropbox\scripts\announce_build_wcn\wcnbuild_release.py", line 733, in   getsanityresults
  dom = minidom.parse(xmlfile)
File "C:\python2.7.3\lib\xml\dom\minidom.py", line 1920, in parse
   return expatbuilder.parse(file)
File "C:\python2.7.3\lib\xml\dom\expatbuilder.py", line 922, in parse
fp = open(file, 'rb')
IOError: [Errno 2] No such file or directory: '<root>\n    <PL name = "MSM8930.LA.2.0-PMIC-8917">\n
5
  • This code won't run. Please indent it correctly. Nov 20, 2012 at 21:31
  • Your function returns a string, not a file object ... I haven't looked at the minidom docs, but that may be important.
    – mgilson
    Nov 20, 2012 at 21:31
  • @mgilson - how can I fix it? Nov 20, 2012 at 21:32
  • You could wrap the string in a StringIO object which behaves like a file in most cases ...
    – mgilson
    Nov 20, 2012 at 21:33
  • its not clear to me...can you show a snippet Nov 20, 2012 at 21:36

2 Answers 2

1

minidom.parse() expects either filename or file-object as a parameter but you are passing the content of the file, try this:

import os
from xml.dom import minidom

doc = minidom.parse(os.path.join('config', productline + '.xml'))

Unless you have specific requirements that favors minidom; use xml.etree.cElementTree to work with xml in Python. It is more pythonic and lxml that you might need in more complex cases supports its API so you don't need to learn twice.

1

I replace the functioncall with file = open('config\\' + productLine + '.xml','r'), it seems to work, why?

You've got two variables, with subtly different names:

xmlfile=xml(productline) // replace with file = open('config\\' + productLine + '.xml','r')

There's productline (lowercase l) and productLine (uppercase L).

If you use the same variable in both cases, you'll likely see more consistent results.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.