0

after running the python script which will actually replace few values from one XML to another XML am seeing IOError: [Errno 13] Permission denied error:

Actual Code:

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)

Error:

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'

Any help is appreciated, thanks!

5
  • Does your script have permissions to access files in that directory? Commented Aug 5, 2019 at 14:04
  • Sorry, how do I give permissions? Commented Aug 5, 2019 at 14:07
  • Can you show the output of D:/DirtRally2/LowSettings/hardware_settings_config.xml as done here: superuser.com/a/364085/524521 Commented Aug 5, 2019 at 14:11
  • @Learner9 How are you running your script? The commands for changing permissions vary based on whether you're using Windows/Linux/Mac Commented Aug 5, 2019 at 14:15
  • am running on win10 and executing the script from IDLE. Commented Aug 5, 2019 at 14:24

1 Answer 1

1

The error here indicates that the file D:/LowSettings/hardware_settings_config.xml cannot be opened for writing. There are several reasons why this may be so and you will need to check them.

  • Does the directory D:/LowSettings exist? (Calling open() does not create directories)
  • Will Windows permissions allow you to write to that directory? (Use idle to create a new file and try to save it to D:/LowSettings under another name.)
  • Does a file with that name already exist? If it exists can you delete it? (Deleting the file shows it isn't locked by another process.)
  • Can you create a file with the name D:/LowSettings/hardware_settings_config.xml with an editor? (Use idle to create a new file and this time save-as your target name. Note that you must do this step after trying to delete the file since having the file open in most editors will lock it - not sure about idle in particular.)

If you can get through all of these checks without success try opening the file interactively from Python's command line.

If all else fails reboot Windows (this clears OS level data structures which today are not often the cause of a problem like this but I'm out of ideas and rebooting won't hurt.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.