I have a script which i want to use to create Write Files to update a serial number on a device.
It does a folder/file path check -> Copies the Read.xml and Creates a Write.xml (Copy) -> Obtains the Serial Number to update using .getroot -> Then i want to update/write a new value to this Write.xml
How do i change the value at root_IM1[1][2].text ?
#Import File Path Verificationimport osimport sysimport os.pathimport timefrom pathlib import Pathimport shutil#XMLimport xml.etree.ElementTree as ET#Shell Commandimport subprocess#Enter Serial No.NAC_Ser = 012345678#Check Folder StructureNAC_FilePath = Path(f"C:\Folder\\{(NAC_Ser}")folp1_2 = Path(f"C:\Folder\{(NAC_Ser}\Folder1")folp2_2 = Path(f"C:\Folder\{(NAC_Ser)}\Folder1\Folder2_1")folp3_2 = Path(f"C:\Folder\{(NAC_Ser)}\Folder1\Folder2_2")if (NAC_FilePath.exists() == True) and (folp1_2.exists() == True) and (folp2_2.exists() == True) and (folp3_2.exists() == True): print("\n") print("Folder Check Passed\n")else: print("\n") print("Failed Folder Check")#Declare Filepaths Globalglobal fp1_2#Check File Existfp1_2 = Path(f"C:\Folder\{(NAC_Ser)}\Folder1\Folder2_1\ReadIm1.xml")if (fp1_2.exists() == True): print("File Check Passed\n") print("XML File Imported\n")else: print("Failed File Check")# Copy Read Files# Rename Read Files (Copy) to Write Files# Write New Values to Write.XML print("\n")print("Creating Write XML Files...")# Source Filepath for Copy -> Destination Filepath -> New File Nameprint("\n")print("Source FilePath to Copy:")print(fp1_2)print("\n")print("New Destination FilePath & New FileName:")dest_fp1 = Path(f"C:\Folder\{(NAC_Ser)}\Folder1\Folder2_1\WriteIM1.xml")print(dest_fp1)shutil.copy2(fp1_2, dest_fp1) # Copy the file#XML Query tree_IM1 = ET.parse(dest_fp1)root_IM1 = tree_IM1.getroot()#IM1 -> IIB PCB Serial (Current)IM1_IIBSerial = (f"{root_IM1[1][2].text}")print(IM1_IIBSerial)#IM1 -> IIB PCB Serial (New Updated)IM1_IIBSerial_New = 12345print(IM1_IIBSerial_New)