I'm writing a log to a simple .txt file and want to read the last few records using Mathematica. But there are some problems.
f = OpenRead["logs.txt"]; Skip[f, ConstantArray[Number, 2], 1]; Read[f, ConstantArray[Number, 2]]
{3, 4}
Add lines and save.
Read[f, ConstantArray[Number, 2]]
EndOfFile
Of course, I can closing file every time, open again, to do Skip
and read updated data. But this is very slow for the large log file.
It should be noted that all lines in my log are aligned to have the specific size. In this example they have size 7 bytes. So I can check the number of records:
FileByteCount["logs.txt"]/7
4
ADDENDUM
SetStreamPosition
is fast. But first run is slower than consequent. For example, on the real data:
f = OpenRead["all_trades.txt"]; (* first run *) SetStreamPosition[f, 0]; SetStreamPosition[f, 3899999]; // AbsoluteTiming (* {0.02188, Null} *) (* second run *) SetStreamPosition[f, 0]; SetStreamPosition[f, 3899999]; // AbsoluteTiming {0.000301056, Null} SetStreamPosition[f, 0]; Skip[f, ConstantArray[Number, 10], 100000]; // AbsoluteTiming (* {0.162914, Null} *) StreamPosition[f] (* 3899999 *) Close[f];
But this does not work with updatable data!
f = OpenRead["logs.txt"]; Read[f, ConstantArray[Number, 2]] (* {1, 2} *) StreamPosition[f] (* 5 *) SetStreamPosition[f, 6]; Read[f, ConstantArray[Number, 2]] (* {3, 4} *) StreamPosition[f] (* 11 *)
Add lines and save.
SetStreamPosition[f, 12]; Read[f, ConstantArray[Number, 2]] (* EndOfFile *)
It looks like a bug. Is not it?
There are no problems in Python.
f = open('logs.txt', 'r') # Add lines and save. f.seek(14) f.read(2)
'05'