Change xml file backup

This commit is contained in:
Esa Kataja
2025-03-13 15:59:01 +02:00
parent e722565199
commit d7f2d048b3
+2 -1
View File
@@ -1,10 +1,11 @@
import lxml.etree as ET import lxml.etree as ET
from pathlib import Path from pathlib import Path
import click import click
from datetime import datetime
def write_xml_data(root: ET.Element, file_path: Path, pretty: bool = True) -> None: def write_xml_data(root: ET.Element, file_path: Path, pretty: bool = True) -> None:
if file_path.exists(): if file_path.exists():
file_path.rename(file_path.with_suffix(".bak")) file_path.rename(str(file_path) + f".{datetime.now().strftime('%Y-%m-%d.%H%M%S')}.bak")
xml_str = ET.tostring(root, encoding="utf-8", xml_declaration=True, pretty_print=pretty) xml_str = ET.tostring(root, encoding="utf-8", xml_declaration=True, pretty_print=pretty)
if pretty: if pretty:
# Parse the XML string to ensure proper indentation # Parse the XML string to ensure proper indentation