0

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?

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Avila
  • 31
  • 6

2 Answers2

2

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

Abhay
  • 46
  • 4
  • Thanks for the help :) The only issue with this method is the path would change depending on the location where the user would have it stored, as well as the project name being different depending on the project – Avila Jun 14 '23 at 17:09
0
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)
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • Thanks for the help! I tested around witht his,a nd it does work, but only if the xml file is on the very top level. Is there a way to get this to work if the file is within a subdirectory? Such as if it is inside the top level of 'projectFolder' as opposed to the root/main folder? – Avila Jun 14 '23 at 16:40
  • @Avila You can use `os.path.join()` to join multiple path components. e.g. `os.path.join(x, 'foo', 'sample.xml')` will get a path to the file sample.xml within foo within x. – Nick ODell Jun 14 '23 at 16:48
  • I think that would work. The projectfolder would be different per project, however in a different def I do a os.walk and have a value that stores the folder name, and could change it into a class with self and get it working that way I believe – Avila Jun 14 '23 at 17:13