[Tutor] File Access

Lloyd Kvam pythonTutor at venix.com
Sun Apr 4 22:13:31 EDT 2004


files (file objects) support the seek method:
	f.seek(pos,how)
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
> var1|var2|var3|var4|var5|var6|var7
> var1|var2|var3|var4|var5|var6
> var1|var2|var3|var4|var5|var6
> 
> 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
> this.
> 
> 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,
> Nick.
> 
> 
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 

Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list