在运行python脚本之后,该脚本实际上将几个XML值替换为另一个XML,出现IOError:[Errno 13]权限被拒绝错误:
实际代码:
import os
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
UN=(os.environ.get('UserProfile'))
actual = ("C:/Users/sam/Documents/hardware_settings_config.xml")
tree.parse ("C:/Users/sam/Documents/hardware_settings_config.xml")
root = tree.getroot()
ID=(root.attrib['MACID'])
tree.parse ("D:/LowSettings/hardware_settings_config.xml")
root = tree.getroot()
root.attrib['MACID'] = ID
tree.write('D:/LowSettings/hardware_settings_config.xml', xml_declaration=True)
import xml.etree.ElementTree as ET
tree = ET.parse("C:/Users/sam/Documents/hardware_settings_config.xml")
root = tree.getroot()
for child in root.findall('opu'):
for sub_c in child:
print (sub_c.attrib)
tree = ET.parse('D:/LowSettings/hardware_settings_config.xml')
root = tree.getroot()
for child in root.findall('opu'):
if child.find('Strategy') is None:
new=ET.SubElement(child,'Strategy')
for key, value in CID.items():
new.set(key, value)
tree.write("D:/LowSettings/hardware_settings_config.xml", xml_declaration=True)
错误:
Traceback (most recent call last):
File "C:\Users\sam\Music\ProfileGen.py", line 13, in <module>
tree.write('D:/LowSettings/hardware_settings_config.xml', xml_declaration=True)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 797, in write
file = open(file_or_filename, "wb")
IOError: [Errno 13] Permission denied: 'D:/LowSettings/hardware_settings_config.xml'
感谢您的帮助,谢谢!
答案 0 :(得分:0)
此处的错误表明无法打开文件D:/LowSettings/hardware_settings_config.xml
进行写入。造成这种情况的原因有很多,您需要检查一下。
D:/LowSettings
是否存在? (调用open()
不会创建目录)D:/LowSettings
。)D:/LowSettings/hardware_settings_config.xml
的文件吗? (使用idle来创建一个新文件,这次将另存为您的目标名称。请注意,您必须在 之后执行此步骤,尝试删除该文件,因为在大多数编辑器中打开该文件都会将其锁定-尤其不确定是否闲置。)如果您无法通过所有这些检查,请尝试从Python的命令行以交互方式打开文件。
如果所有其他方法都无法重新启动Windows(这清除了OS级别的数据结构,这些数据结构今天通常不是造成此类问题的原因,但是我没有主意,重新启动不会受到损害。)