Im new to python and got stuck while processing a .bat file
1) I have a .bat file that contains the following lines:
set local_dir=%~d0%~p0
set LVMain_StartAddress=0xfaff
set LVMain_EndAddress=0x0f4ff
set HVMain_StartAddress=0x01d00
set HVMain_EndAddress=0x095ff
set LVMain_StartAddressMerged=0x0D000
set LVMain_EndAddressMerged=0x0fcff
set HVMain_StartAddressMerged=0x01001d00
set HVMain_EndAddressMerged=0x010095dd
2) A python script runs through the file to look for the line "set LVMain_StartAddress=0xfaff
"
3) When it finds the line, I want to replace the hex address with a new one. Sometimes the new hex address is 5 characters long
4) Below is the python code I wrote to do this:
# write LVMain start address in .bat file
with open("..\..\..\Tools\Scripts\GenerateRomTestChecksums.bat", "r+") as f:
#read lines individually until we find where to write LvMain Start address
BatFileLine = f.readline()
while( (LVMain_StartPattern in BatFileLine ) == False ):
BatFileLine = f.readline()
#set marker back to the beginning of the line
LineMarker = int(f.tell()) - (int(len(BatFileLine)) + 0x01)
f.seek(LineMarker)
#split string where address starts to write new address in file
name, hex_address = BatFileLine.split('0x')
#append new address and carriage return
name = name + APPL_End_address + "\n"
print(len(name))
contents = f.read(len(BatFileLine)).replace(BatFileLine,name)
f.seek(LineMarker)
f.write(name)
This is part of a larger function and "APPL_End_address
" is given as a parameter
5) When I run this code, and the new hex address is 5 hex digits wide, I get the following output:
It seems the new hex address is written correctly, but the first character of the next line erased. Any ideas why?