Read file from bottom
Brian Birkinbine
bbirkinbine at earthlink.net
Mon May 13 19:55:12 EDT 2002
I'm fairly new to python as well. I was brainstorming a non tail specific way of attacking this
without actually reading through the entire file (even with caching to conserve memory)
Assumptions:
linesize throughout file is uniform/close enough (line 1 is similarly long as last line)
and doesn't have to start on a newline boundary (but I guess you could pick an
arbitrary point (10% from the end), read into buffer, then search forward to the
newline, then start normal line processing.
Python example code:
import os
# add error checking, etc to make production ready
filename = "YOURFILENAMEGOESHERE"
inputfd = file(filename,'rb')
# read in one line, calculate line size
# can be hardcoded if data is uniform or a percentage of the total filesize, etc...
linesize = len(inputfd.readline())
#grab filesize from os.stat
st = os.stat(filename)
filesize = st.st_size
# num of lines backward
numlines = 20
# calculate start point from end of file
startpoint = filesize - (numlines * linesize)
# seek to calculated startpoint
inputfd.seek(startpoint)
# read file from startpoint to end into buffer
# could also just do readline.
buffer = inputfd.read()
inputfd.close()
#YOUR PROCESSING GOES HERE ...
On Mon, May 13, 2002 at 05:20:34PM -0400, Hardy Merrill wrote:
> You probably already know this, but on the chance you don't,
> you can add a parameter to tail to tell it how many lines from
> the bottom you want. For example, if I want 35 lines from the
> bottom I can do
>
> tail -35 <filename> | python script.py
>
> Julia Bell [julia.bell at jpl.nasa.gov] wrote:
> > Good idea; I never thought of something like that.
> >
> > My initial reaction is that I don't know I exactly how many lines frm the
> > bottom I want. (I currently read the lines, search for data on the lines
> > that meets some trigger criterion, and then start processing the data from
> > there.) But, there still might be something worth thinking about regarding
> > whether it is more efficient to pre-process the data (similar to using
> > 'tail') than to process it as part of my script.
> >
> > Thanks.
> >
> > Julia Bell
> >
> >
> > William Park wrote:
> >
> > > Julia Bell <julia.bell at jpl.nasa.gov> wrote:
> > >
> > > >
> > > > Is there an efficient (and relatively easy - I'm new to Python) way of
> > > > reading the file from the last line backward instead of the first line
> > > > forward?
> > >
> > >
> > > tail | python script.py
> > >
More information about the Python-list
mailing list