How to quickly get the last line from a .csv file over a network drive?

user1367204 picture user1367204 · Jun 21, 2017 · Viewed 15.3k times · Source

I store thousands of time series in .csv files on a network drive. Before I update the files, I first get the last line of the file to see the timestamp and then I update with data after that timestamp. How can I quickly get the last line of a .csv file over a network drive so that I don't have to load the entire huge .csv file only to use the last line?

Answer

Douglas picture Douglas · Jun 21, 2017

There is a nifty reversed tool for this, assuming you are using the built-in csv module:

how to read a csv file in reverse order in python

In short:

import csv
with open('some_file.csv', 'r') as f:
    for row in reversed(list(csv.reader(f))):
        print(', '.join(row))

In my test file of:

1:   test, 1
2:   test, 2
3:   test, 3

This outputs:

test, 3
test, 2
test, 1