Etree - write file to gateway

Hi. I’m trying to write an xml file to ‘C:\Folder’ on the gateway.

This is the xml:

root = ET.Element("root")
doc = ET.SubElement(root, "doc")
			
ET.SubElement(doc, "field1", name="blah").text = "some value1"
ET.SubElement(doc, "field2", name="asdfasd").text = "some vlaue2"
			
tree = ET.ElementTree(root)
tree.write("test.xml")

how can I write that file to the gateway ?
I’m using WebDev and I’ve setup a folder resource named C:\Folder\

Thanks.
btw, python is not my strongest language.

Found the solution… I was using backslashes, which didn’t work… changed to forward slash, and now it works like a charm.

root = ET.Element(“root”)
doc = ET.SubElement(root, “doc”)

ET.SubElement(doc, “field1”, name=“blah”).text = “some value1”
ET.SubElement(doc, “field2”, name=“asdfasd”).text = “some vlaue2”

tree = ET.ElementTree(root)
tree.write(“C:/Folder/test.xml”)

1 Like

If you want to use back slashes, you need to escape them as these are special characters in Python.
e.g.

tree.write("C:\\Folder\\test.xml")

Then again, it’s far easier just to use forward slashes :slight_smile:

2 Likes

tree.write(r"C:\Folder\test.xml")

Or use raw strings :stuck_out_tongue:

2 Likes

Right!!! I keep forgetting that. Really handy when I have to deal with UTF-8 characters.