Skip to content

Instantly share code, notes, and snippets.

@drsxr
Created July 19, 2022 22:28
Show Gist options
  • Save drsxr/099094ca7b8e266e9df8e3668ab68929 to your computer and use it in GitHub Desktop.
Save drsxr/099094ca7b8e266e9df8e3668ab68929 to your computer and use it in GitHub Desktop.
Get last line of file (csv) without reading the entire file into memory
#This works. Yields the last line of the file as a string without reading the entire file into memory.
#Use a parsing function to separate returned data by commas to select the desired element.
import os
def get_last_line_records_of_file(identifier_data):
filepath = linux_path+"/"+identifier_data+".csv"
try:
with open(filepath,'rb') as f:
f.seek(-1,os.SEEK_END)
text = [f.read(1)]
while text[-1] != '\n'.encode('utf-8') or len(text)==1:
f.seek(-2, os.SEEK_CUR)
text.append(f.read(1))
except Exception as e:
print("EXCEPTION: ",e)
pass
return ''.join([t.decode('utf-8') for t in text[::-1]]).strip()
@drsxr
Copy link
Author

drsxr commented Jul 19, 2022

Ever want to get the last record in a csv dataset without reading in the entire dataset? Dataset too big to fit into memory? Just need to know the date of the last record to know where to append next? You're welcome. Solution not by me but via stack overflow adapted for my needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment