简体   繁体   中英

Can I use python to open an XML file, format it, then save it again?

Hello Python Developers,

I want to write a python script that will search a directory for any files with the extension ".err", then format the file (which is an XML format), then overwrite the file with the correct formatting. Here is the code I have so far:

import xml.dom.minidom
import glob
import os

path = "/qond/apps/tomcat_qx/webapps/qxiqonddb/qxtendQueues/qdocqueue/responses/"

os.chdir(path)
for file in glob.glob("*.err"):

    with open(path + file) as f:
        xml_data = f.read()
        xml = xml.dom.minidom.parse(xml_data)
        xml_formatted = dom.toprettyxml()
        f.write(xml_formatted)
        f.close()

Many Thanks in advance!

Edit: The current issue I face with the above code is:

Traceback (most recent call last):
  File "qxtend_formatter.py", line 12, in <module>
    xml = xml.dom.minidom.parse(xml_data)
  File "/usr/lib64/python2.6/xml/dom/minidom.py", line 1918, in parse
    return expatbuilder.parse(file)
  File "/usr/lib64/python2.6/xml/dom/expatbuilder.py", line 922, in parse
    fp = open(file, 'rb')
IOError: [Errno 36] File name too long: '<?xml version="1.0" encoding="UTF-8"?>\n<soapenv:Envelope xmlns:soapenv="http://schemas.xm.....

It seems as though it tries to save the file name as the file contents, but I would like it to keep whatever filename is had.

I have resolved this issue by doing two things:

  1. Ensuring that the OS user has full access to the file (chmod 777)
  2. Creating an '.f' instance to read and a '.fl' instance to write the file

My code now looks like this:

from xml.dom import minidom
import glob
import os

path = "/qond/apps/tomcat_qx/webapps/qxiqonddb/qxtendQueues/qdocqueue/responses/"

os.chdir(path)
for file in glob.glob("*.err"):
    with open(file, "r") as f:
        xml_data = f.read()
        doc = minidom.parseString(xml_data)
        with open(file, "w") as fl:
            fl.write(doc.toprettyxml(indent="  ", newl="\n"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM