[Tutor] File Access
pythonTutor at venix.com
Sun Apr 4 22:13:31 EDT 2004
files (file objects) support the seek method:
pos is the position
how is 0 for start
1 for current location
2 for end
f.seek(0,2) would place you at the end of the file.
Note that text mode in Windows can confuse things. binary mode and unix
will work as you expect.
On Sun, 2004-04-04 at 13:26, Nick Lunt wrote:
> Hi folks,
> I need to go to the last line of a file and then to the start of a
> delimited section of that line, such as
> $ cat file.txt
> What I want to do is goto the last line in the imaginary file.txt above
> and extract var3 for example.
> I could probably do a realines() until EOF overwriting a variable with
> the contents of each line so that at the end the variable will hold the
> contents of just the last line, or by putting each line of the file into
> a list and pulling the last element of the list out, but eventually the
> file.txt will get pretty big, so I thought of using seek().
> However, I could not find on python.org or by googling any way to do
> So can anyone please give me a hint on the most efficient way to grab
> the contents of the last line of a large text file please ?
> Kind regards,
> Tutor maillist - Tutor at python.org
1 Court Street, Suite 378
Lebanon, NH 03766-1358
More information about the Tutor