Praful Bagai
Praful Bagai

Reputation: 17392

Parsing the json file after a specific time interval

I've a json file and this file will be updated every 5 mins or so. Initially I parse the file from the starting point. Now I need to parse this json file after every 15 to 20 mins. Is there any way of storing the pointer kind of thing which will store the last line of the file parsed and when again after 15/20 mins I need to parse the file it should start from that pointer(Since parsing the same data(historical data) would be highly inefficient and would make my process go slow)?

Upvotes: 1

Views: 376

Answers (2)

Parth Gajjar
Parth Gajjar

Reputation: 1424

Use tell() method of file(after reading from file) this will return current pointer. And next time you read use seek() function of file for setting pointer to old position.

Example:

f = open("test.json" , "w+")
 .....
 .....
your code for reading 
f.read()
 .....
 .....
last_position = f.tell() # return current position of file pointer(where you stoped reading)

now when you next time read from file use seek() function

f = open("test.json" , "w+")
f.seek(last_position)
f.read() # now this will start reading from last position

Hope This will Help :)

Upvotes: 2

Yussuf S
Yussuf S

Reputation: 2112

see the python doc for linecache

Upvotes: 0

Related Questions