TypeError:write()得到意外的关键字参数“pretty\u print”

2024-10-06 12:27:10 发布

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

我正在编写一个python脚本,它将在jenkins作业的config.xml中附加一个新的Tag/elment。

我的剧本就是这样的:

#!/usr/bin/python

import os, fnmatch, pdb, re, string, fileinput, sys
from lxml import etree
def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
    for name in files:
        if fnmatch.fnmatch(name, pattern):
            result.append(os.path.join(root, name))
return result

finalresult = find('config.xml', './')
print finalresult
def writexml(filepath):
tree = etree.parse(filepath)
root = tree.getroot()
a=[]
for v in root.iter('publishers'):
        for a in v:
            if a.tag == "hudson.plugins.emailext.ExtendedEmailPublisher":
                t1=etree.SubElement(v,'org.jenkinsci.plugins.postbuildscript.PostBuildScript',{'plugin':"postbuildscript@017"})
                t2=etree.SubElement(t1,"buildSteps")
                t3=etree.SubElement(t2,'hudson.tasks.Shell')
                t4=etree.SubElement(t3,"command")
                t4.text = "bash -x /d0/jenkins/scripts/parent-pom-enforcer.sh"
                t5 = etree.SubElement(t1,'scriptOnlyIfSuccess')
                t5.text = "false"
                t6 = etree.SubElement(t1,'scriptOnlyIfFailure')
                t6.text = "false"
                t7= etree.SubElement(t1,'markBuildUnstable')
                t7.text = "true"

tree.write(filepath,pretty_print=True)

findMavenProject=[]
for i in finalresult:
tree = etree.parse(i)
root = tree.getroot()
for v in  root.iter('hudson.tasks.Maven'):
    if v.tag == "hudson.tasks.Maven":
        writexml(i)
        findMavenProject.append(i)
print findMavenProject

执行此脚本时,出现以下错误:

  running with cElementTree on Python 2.5+
  ['./jen1/config.xml', './jen2/config.xml', './jen3/config.xml',     './jen4/config.xml']
  Traceback (most recent call last):
  File "./find-test.py", line 50, in <module>
  writexml(i)
  File "./find-test.py", line 41, in writexml
  tree.write(filepath,pretty_print=True)
  TypeError: write() got an unexpected keyword argument 'pretty_print'

我搜索了这个错误,发现我应该使用“lxml”。我用过它,但即使在那之后我也会犯同样的错误。我使用的是Python2.7.6版本。。

有线索吗?


Tags: textinconfigtreeforrootxmlfind