-1

I have some .xml files I need to convert into .txt files programmatically for a customer. There is no need to parse the xml tree to return the text values. I literally just need to change the file extension from .xml to .txt. The converted file would contain all of the xml tree including tags etc. The customer then wants to parse this later on.

So far I have:

import xml.etree.ElementTree as ET
tree = ET.parse('myfile.xml')
root = tree.getroot()
with open('myfile.txt', 'w') as f:
    f.write(root)
f.close()

Which is returning an error of:

Traceback (most recent call last):
  File "C:/Users/myuser/Documents/Python 3 Scripts/test.py", line 5, in <module>
    f.write(root)
TypeError: write() argument must be str, not xml.etree.ElementTree.Element

What is the required fix to resolve?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
gdogg371
  • 3,879
  • 14
  • 63
  • 107
  • only fighting fire with fire...it's a frequent pattern on here...a lot of people's opening salvo when attempting to 'help' comes across as having more of an interest in trying to make the OP feel bad than actually doing anything to help...would you want to be spoken to the way you opened up? there are some genuinely great people on here though...if one of them would like to assist me that would be great... – gdogg371 Nov 16 '18 at 14:56
  • Apology accepted. There is general perception of SO being a bit of unkind place at times...see Idlehand's answer below. That was all that was needed. I've never converted a file type before in Python and had obviously gone in the wrong direction looking for a solution... – gdogg371 Nov 16 '18 at 15:06
  • See also: https://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python – Micha Wiedenmann Nov 19 '18 at 08:41

1 Answers1

1

Just read the data as string (or bytes if you want) and write it out as a different extension...

with open('myfile.xml', 'r') as file_in, open('myfile.txt', 'w') as file_out:
    data = file_in.read()
    file_out.write(data)

Or if you really just want to change the current file's extension:

import os
os.rename('myfile.xml','myfile.txt')
r.ook
  • 13,466
  • 2
  • 22
  • 39
  • thank you very much for taking the time to respond. this did exactly what I needed. changing file extensions is not something I have gone before in python. – gdogg371 Nov 16 '18 at 15:07
  • I do concede sometimes the community can be a bit jaded (myself included) of the influx of questions that feels like a lazy dump (and these are not infrequent). But at times there are genuinely questions with simple solutions that just unfortunately escaped the OP like in your case. I will say though the conception goes both ways - there are some condescending answerers, but there are also some (if not many) lazy, demanding questions without putting in effort (again, not your case). – r.ook Nov 16 '18 at 15:09
  • agreed on all counts. normally i just play along if someone is coming across as stroppy when responding, but today i am having a bad day and my patience deserted me... – gdogg371 Nov 16 '18 at 15:12