import os
import xml.etree.ElementTree as ET
def changesku(inputpath):
listdir = os.listdir(inputpath)
for file in listdir:
if file.endswith('xml'):
file = os.path.join(inputpath,file)
tree = ET.parse(file)
root = tree.getroot()
for object1 in root.findall('object'): #我要修改的元素在object里面,所以需要先找到object
for sku in object1.findall('name'): #查找想要修改的所有同种元素
if (sku.text == '005'): #‘005’为原始的text
sku.text = '008' #修改‘name’的标签值
tree.write(file,encoding='utf-8') #写进原始的xml文件,不然修改就无效,‘encoding = “utf - 8”’避免原始xml #中文字符乱码
else:
pass
else:
pass
if __name__ == '__main__':
inputpath = 'D:\\easy\\hebing_xml' #这是xml文件的文件夹的绝对地址
changesku(inputpath)