I have the following code and it works fine:
with open(input_file, 'rb') as f:
#may be a large file, seeking without iterating
f.seek(-2, os.SEEK_END)
while f.read(1) != b'\n':
f.seek(-2, os.SEEK_CUR)
last_line = f.readline().decode()
last_record = json.loads(last_line)
Now, I do not want the last line and want to remove it from the original file, and then the original 2nd last line becomes the last line & assign to last_record
. How should I modify above code to make it happen?
The input_file
can be very big and there can be millions of rows. I do not want to create any temporary variable just for removing the last line.