0

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
xzk
  • 827
  • 2
  • 18
  • 43
  • @DavidS No, I do not want to store the all lines into something, as the file can be very big, a few gigabytes with millions of rows. – xzk Oct 11 '20 at 10:07
  • 2
    refer to this specific answer https://stackoverflow.com/a/10289740/8890604 – David Oct 11 '20 at 10:08
  • Generally when doing research on SO you should look through more than just the accepted answer; the OP's constraints may be different to yours. – jonrsharpe Oct 11 '20 at 10:11
  • @DavidS Thanks for this. Unfortunately this specific answer did not work. The last line was not removed at all. No error was observed though. – xzk Oct 11 '20 at 10:17

0 Answers0