0

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:
result

It seems the new hex address is written correctly, but the first character of the next line erased. Any ideas why?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Alex
  • 57
  • 2
  • 8
  • This way of iterating over the lines is way too complicated and easily leads to mistakes such as what you have encountered here. You should just use `for line in file` and output either the original or a modified line to a new file. – mkrieger1 Apr 08 '20 at 19:30
  • 3
    I know nothing about python, but it seems you don't work with lines, but with byte positions. So when you write a line that is longer than the original, it will overwrite the start of the next line. – Stephan Apr 08 '20 at 19:31
  • you could read all text to memory (and close file), use `all_text.replace("set LVMain_StartAddress=0xfaff", "set LVMain_StartAddress=new_address")` , and write all back to the same file. – furas Apr 08 '20 at 21:48

1 Answers1

2

There are many ways to solve this problem. Many times just writing a simpler algorithm can solve your problem as there are less pitfalls for error.

In my example we simply iterate through each line of the input file, if the line matches the criteria to replace, then we replace, and write it in our new file. Otherwise we leave the line the same and write it to the new file. Nice and simple.

This method will work regardless of how long your APPL_End_Address input string is, which as suggested above, is likely causing an issue when it is longer than the original.

with open("Fileout.txt",'w') as f:
for line in inputFile:
    if "LVMain_StartAddress=" in line:
        line = line.replace(line.split("=")[1],APPL_End_address)
        f.write("%s\n" % line)
    else:
        f.write("%s" % line)
Meathusk
  • 34
  • 4
  • While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Apr 09 '20 at 01:11
  • Thanks for your help..Your solution works great – Alex Apr 13 '20 at 16:53