没有这样的文件或目录:'<root>\n

2024-09-24 22:30:13 发布

您现在位置:Python中文网/ 问答频道 /正文

每当有一个对xml(productline)的函数调用时,我都会遇到以下错误,但是如果我用file = open('config\\' + productLine + '.xml','r')替换函数调用,它似乎可以工作,为什么?在

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

错误:

^{pr2}$

Tags: configdatadef错误withlinexmlopen
2条回答

^{}需要文件名或文件对象作为参数,但您正在传递文件的内容,请尝试以下操作:

import os
from xml.dom import minidom

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

除非您有支持minidom的特定需求,否则请使用^{}在Python中处理xml。在更复杂的情况下,您可能需要更python和^{}支持它的API,因此您不需要学习两次。在

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

有两个变量,它们的名称略有不同:

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

productline(小写l)和{}(大写L)。在

如果在这两种情况下使用相同的变量,则可能会看到更一致的结果。在

相关问题 更多 >