Here is the content of my file
I want to replace all occurrence of pyt_batch_id with any number but not the first occurrence.
I tried the below method and it is working as expected but I don't think this the best approach.
s = open("BATCH_ROLLBACK.txt").read()
s = s.replace('pyt_batch_id', '123456')
f = open("BATCH_ROLLBACK.txt", 'w')
f.write(s)
f.close()
f2 = open('BATCH_ROLLBACK.txt', 'r')
contents = f2.read().replace('123456', 'pyt_batch_id',1)
f2.close()
f2 = open('BATCH_ROLLBACK.txt', 'w')
f2.write(contents)
f2.close()
output :-
Could anyone please suggest other alternative methods?
Found similar question but that is for a line not for file. How to replace all occurences except the first one?