未创建 xml 文件的问题

问题描述 投票:0回答:1

在此代码中,打印(计数)语句也未显示,也没有创建 xml 文件。为什么未创建 xml 文件是 raise 语句的问题。 代码-----

import os
import xml.etree.ElementTree as ET

error_messages = []
count = 0

def sample_function():
    try:
        result = 10 / 0  
    except Exception as e:
        print(f"An error occurred: {e}")
        error_messages.append(str(e))
        global count
        count += 1
        print(count)
        raise Exception("An error occurred in the script.")

sample_function()

if count > 0:
    print("Creating XML...")
    root = ET.Element("errors")
    subject = ET.Element("subject")
    subject.text = "DS Interface-v2.2.1 - ERROR: Python Script"
    salutation = ET.Element("salutation")
    salutation.text = "Dear Sir/Madam,"
    body = ET.Element("body")
    logs_path = "${VMPROCESSES}\\TRUMPF\\DS-Interface-v2.2.1\\python\\logs"
    body.text = f"The Process has not been finished successfully. Having some issues regarding the Python script. Logfile can be found at location {logs_path}"
    root.append(subject)
    root.append(salutation)
    root.append(body)
    for error_message in error_messages:
        error_element = ET.Element("error")
        error_element.text = error_message
        root.append(error_element)

    output_directory = "c:/Users/PC/Desktop/gathering for ds/all-try python file/sample"
    
    os.makedirs(output_directory, exist_ok=True)
    
    xml_file_path = os.path.join(output_directory, "sample1_messages.xml")

    tree = ET.ElementTree(root)
    tree.write(xml_file_path)
    print(f"XML file created at {xml_file_path}")

为什么 xml 文件没有创建是 raise 语句的问题。

python exception logging elementtree
1个回答
0
投票

您可以将 if 语句放在 finally 块中。

import os
import xml.etree.ElementTree as ET

error_messages = []

def sample_function(counter=0):

    try:
        result = 10 / 0
        print(result)
    except ZeroDivisionError as e:
        error_messages.append(str(e))
        print(e)
        counter += 1
            
    except Exception as e:
        print(f"An error occurred: {e}")
        error_messages.append(str(e))
        counter += 1
        raise Exception("An error occurred in the script.")
        
    finally:
        if counter > 0:
            print("Creating XML...")
            root = ET.Element("errors")
            subject = ET.Element("subject")
            subject.text = "DS Interface-v2.2.1 - ERROR: Python Script"
            salutation = ET.Element("salutation")
            salutation.text = "Dear Sir/Madam,"
            body = ET.Element("body")
            logs_path = "${VMPROCESSES}\\TRUMPF\\DS-Interface-v2.2.1\\python\\logs"
            body.text = f"The Process has not been finished successfully. Having some issues regarding the Python script. Logfile can be found at location {logs_path}"
            root.append(subject)
            root.append(salutation)
            root.append(body)

            for error_message in error_messages:
                error_element = ET.Element("error")
                error_element.text = error_message
                root.append(error_element)

            ET.dump(root)

            output_directory = "sample"

            os.makedirs(output_directory, exist_ok=True)

            xml_file_path = os.path.join(output_directory, "sample1_messages.xml")

            tree = ET.ElementTree(root)
            tree.write(xml_file_path)
            print(f"XML file created at {xml_file_path}")
            
            
sample_function(0)
© www.soinside.com 2019 - 2024. All rights reserved.