Skip to content

Instantly share code, notes, and snippets.

@ReeganArockiasmy
Created October 23, 2020 12:11
Show Gist options
  • Save ReeganArockiasmy/fe4f20bcafadf74ebc15803553e8bc3c to your computer and use it in GitHub Desktop.
Save ReeganArockiasmy/fe4f20bcafadf74ebc15803553e8bc3c to your computer and use it in GitHub Desktop.
#python #snippet #replace
import fileinput
file1 = fileinput.input('CanMsgParser.CPP', inplace=True)
deleteflag = True
x = iter(range(0,100))
startdel = "// ==== Rx Error Generation Start ==="
enddel = "// ==== Rx Error Generation End ==="
def find_and_replace(startString,endString,fileName,replaceString):
'''
:param startString: Must be a unique of the string
:param endString: Must be a unique of the string
:param fileName:
:param replaceString: it is iterated string
:return:
'''
fp = fileinput.input(fileName,inplace=True)
for line in fp:
if startString in line:
while endString not in next(fp): # This while loop deleting the block of code
pass
for replaceline in replaceString: # This for loop adding the block of code
print replaceline
else:
print line, # Adding remaining line in file
find_and_replace(startdel,enddel,"CanMsgParser.CPP",range(0,19))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment