英文:
How to force ElementTree to look in a specific directory for xml files?
问题
I have been trying to get ElementTree(ET) to look for xml files in a specific directory which the user will point to, however I found out that ET is only searching for that xml file in the directory of the script itself. How can I force ET to look in the right directory, and why is it ignoring the one that is passed down to it?
The code is split into two different scripts.
main.py:
#This script is mainly a tkinter script with buttons/entries. One button takes the given directory and passes it over to a function that launches the second script/function. Including this here to better make sense of how the code will be working
.
.
def clicked(self):
check_xml(self.directory)#the value that is stored from a different button
classes.py:
import os
import glob
import xml.etree.ElementTree as ET
def check_xml(x):
tree = ET.parse('sample.xml')
root = tree.getroot()
print(tree)
The folder structure is as follows:
root/main folder(This is what 'self.directory' and 'x' will point to):
|--projectFolder
|--subfolder
|--anotherXML.xml
|--sample.xml
Eventually I would go into the other xml's but right now I'm trying to work my way up(down?) by making sure the code works correctly and displays the information I want.
Why is it that 'x' gets ignored by the function, and instead only checks the directory that the code itself is in only, and how can I get it to work correctly?
英文:
I have been trying to get ElementTree(ET) to look for xml files in a specific directory which the user will point to, however I found out that ET is only searching for that xml file in the directory of the script itself. How can I force ET to look in the right directory, and why is it ignoring the one that is passed down to it?
The code is split into two different scripts.
main.py:
#This script is mainly a tkinter script with buttons/entries. One button takes the given directory and passes it over to a function that launches the second script/function. Including this here to better make sense of how the code will be working
.
.
def clicked(self):
check_xml(self.directory)#the value that is stored from a different button
classes.py:
import os
import glob
import xml.etree.ElementTree as ET
def check_xml(x):
tree = ET.parse('sample.xml')
root = tree.getroot()
print(tree)
The folder structure is as follows:
root/main folder(This is what 'self.directory' and 'x' will point to):
|--projectFolder
|--subfolder
|--anotherXML.xml
|--sample.xml
Eventually I would go into the other xml's but right now I'm trying to work my way up(down?) by making sure the code works correctly and displays the information I want.
Why is it that 'x' gets ignored by the function, and instead only checks the directory that the code itself is in only, and how can I get it to work correctly?
答案1
得分: 2
检查 x 中是否包含正确的文件路径以及文件名 "sample.xml"。
例如:
x = 'C:\Users\stdev\Desktop\am\sample.xml'
然后只需调用 x 到 ET.parse()。
示例:
tree = ET.parse(x)
希望这有所帮助。
英文:
check if x contains correct file path along with the file name "sample.xml"
in it.
For Example:
x = 'C:\\Users\\stdev\\Desktop\\am\\sample.xml'
and then just call x into ET.parse()
example:
tree = ET.parse(x)
hope this helps
答案2
得分: 0
def check_xml(x):
tree = ET.parse(os.path.join(x, 'sample.xml'))
root = tree.getroot()
print(tree)
英文:
def check_xml(x):
tree = ET.parse('sample.xml')
root = tree.getroot()
print(tree)
The file sample.xml
is a relative path, and is interpreted relative to the current working directory. You want an absolute path.
Assuming self.directory
contains an absolute path to a directory where you want to find sample.xml
, you can use os.path.join
to get an absolute path to sample.xml
.
Example:
def check_xml(x):
tree = ET.parse(os.path.join(x, 'sample.xml'))
root = tree.getroot()
print(tree)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论