def modifyip(tfile, deviceAddress): # tfile For the target file ,deviceAddress For the criteria to find
desktop_path = 'C:/Users/JiajunHu/Desktop/' # The newly created txt File storage path
full_path = desktop_path + deviceAddress + '.txt' # With deviceAddress For the file name
try:
lines = open(tfile, 'r', encoding='utf-8').readlines()
flen = len(lines) - 1
print(flen)
for i in range(flen):
if deviceAddress in lines[i]:
file = open(full_path, 'a')
file.write(lines[i - 5] + "\n" +lines[i - 4] + "\n" +
lines[i - 3]+ "\n" + lines[i] + "\n" + lines[i + 4])
except Exception as e:
print(e)
if __name__ == '__main__':
modifyip('C:/Users/JiajunHu/Desktop/log_4.txt',
'8akDIW5BSG0U') # stay 'C:/Users/JiajunHu/Desktop/log_4.txt' Search for '8akDIW5BSG0U'