سؤال

import xml.etree.ElementTree as ET

ID="000296166"
tree = ET.parse("\folder" + ID +'.xml')
root = tree.getroot()

What I'm trying to do is access XML files that aren't in the same folder as the .py using the function from xml.etree.elementtree

It keeps giving the error:

IOError: [Errno 22] invalid mode ('rb') or filename: '\x0colder000296166.xml'

I keep getting the feeling that I did something wrong when writing the path...but I can't find any examples online to see how it's supposed to work

هل كانت مفيدة؟

المحلول

\f is interpreted as the page brake and is replaced with hex code 0xC. You should remove leading backslash from path.

tree = ET.parse("folder" + ID +'.xml')

And if you use backslash inside strings it can be escaped like this \\

EDIT

When you work with paths it is better to use os.path module:

 import os 
 ...
 tree = ET.parse(os.path.join('folder', ID + '.xml'))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top