Re: [Tutor] Trying to extract the last line of a text file

2006-10-20 Thread János Juhász
> Danny Yoo wrote: > > > > file('filename.txt').readlines()[-1] > Not to hijack the thread, but what stops you from just putting a > file.close() after your example line? > >>> Which file should file.close() close? The problem is that we don't > >>> have a handle on the particular fi

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Chris Hengge
Oops, my mistake, I did read your code, as well as all the others and I had your code mixed with another submission. In that case, excellent contribution =DOn 10/19/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote: Chris Hengge wrote:> More on that.. some of the file I work with are thousands of li

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Luke Paireepinart
Chris Hengge wrote: > More on that.. some of the file I work with are thousands of lines > long... one is even 10's of thousands.. so reading the entire thing > into ram is MUCH faster then reading line by line with the filestream > open. Did you look at my code? It doesn't read the whole thing

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Chris Hengge
More on that.. some of the file I work with are thousands of lines long... one is even 10's of thousands.. so reading the entire thing into ram is MUCH faster then reading line by line with the filestream open. On 10/19/06, Chris Hengge <[EMAIL PROTECTED]> wrote: I dont care for slow... I dont use

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Chris Hengge
I dont care for slow... I dont use computers with less then 1gb of ram.. (all my systems have 2gb), I hate to wait... =D If I've got memory to use, I intend to use it!As for reading 100 20mb files, I'd do one at a time, then dump the variable storing the data, or reset/re-use. Just my take though.

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Luke Paireepinart
Chris Hengge wrote: > I thought my solution was the easiest.. but I guess everyone skipped it =P No, we didn't skip it, but as we're all programmers here, we showed alternate ways that it could be done. Your post is the one that sparked the whole 'garbage collection' thing, you'll notice. Now, I

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Chris Hengge
I thought my solution was the easiest.. but I guess everyone skipped it =POn 10/19/06, Kent Johnson <[EMAIL PROTECTED] > wrote:Danny Yoo wrote:>> file('filename.txt').readlines()[-1] Not to hijack the thread, but what stops you from just putting a file.close() after your example line?>

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Kent Johnson
Danny Yoo wrote: > > file('filename.txt').readlines()[-1] Not to hijack the thread, but what stops you from just putting a file.close() after your example line? >>> Which file should file.close() close? The problem is that we don't >>> have a handle on the particular file we want

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Asrarahmed Kadri
Danny This is a very clean way to get the last line.   Thanks. Regards, Asrarahmed  On 10/19/06, Danny Yoo <[EMAIL PROTECTED]> wrote: > first count the number of lines in the file by using a loop. Use a> second loop and when teh counter reaches the num_of_lines values: take > the line.>> Is th

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Chris Hengge
Very informative and creative reply! Thanks for sharing :]On 10/19/06, Danny Yoo <[EMAIL PROTECTED] > wrote:>> >> file('filename.txt').readlines()[-1]>> >> > Not to hijack the thread, but what stops you from just putting a>> > file.close() after your example line? Which file should file.close()

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Danny Yoo
>> >> file('filename.txt').readlines()[-1] >> >> > Not to hijack the thread, but what stops you from just putting a >> > file.close() after your example line? >> >> Which file should file.close() close? The problem is that we don't >> have a handle on the particular file we want to close off

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Danny Yoo
> This works as well > > file('filename.txt').readlines()[-1] > > Some will say that this is no good because the file is still open. > However I've been told that when the object is cleaned the file is > closed so it should be fine. It matters in an implementation like Jython, which depends on t

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Chris Hengge
Oh wow.. I totally missed that... nevermind.. ignore that question =DOn 10/19/06, Danny Yoo <[EMAIL PROTECTED] > wrote:>> file('filename.txt').readlines()[-1]> Not to hijack the thread, but what stops you from just putting a > file.close() after your example line?Which file should file.close() clos

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Danny Yoo
>> file('filename.txt').readlines()[-1] > Not to hijack the thread, but what stops you from just putting a > file.close() after your example line? Which file should file.close() close? The problem is that we don't have a handle on the particular file we want to close off. ___

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Danny Yoo
> first count the number of lines in the file by using a loop. Use a > second loop and when teh counter reaches the num_of_lines values: take > the line. > > Is there any other way to do it?? Yes, there's a way to do it in one pass: you can keep track of the very last line you've read from the

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Jordan Greenberg
Chris Hengge wrote: > Not to hijack the thread, but what stops you from just putting a > file.close() > after your example line? Because file is a class. file(filename) creates a file object, but he's not saving the object to a variable, so its lost. file.close() takes a file object as a parameter

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Chris Hengge
Not to hijack the thread, but what stops you from just putting a file.close() after your example line?On 10/19/06, Chad Crabtree < [EMAIL PROTECTED]> wrote:file('filename.txt').readlines()[-1] Some will say that this is no good because the file is still open.  However I've been told that when the o

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Chad Crabtree
On 10/19/06, Chris Hengge <[EMAIL PROTECTED]> wrote: I'd personally do something like this. file = open(myfile, 'r')fileContents = file.readlines() # read the entire document into memory for speed.file.close()print fileContents[-1] # This is the last line. This works as wellfile('filename.txt').rea

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Brian van den Broek
Asrarahmed Kadri said unto the world upon 19/10/06 12:55 PM: > My algorithm is like this: > first count the number of lines in the file by using a loop. > Use a second loop and when teh counter reaches the num_of_lines values: > take > the line. > > Is there any other way to do it?? > > > > On

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Chris Hengge
I'd personally do something like this. file = open(myfile, 'r')fileContents = file.readlines() # read the entire document into memory for speed.file.close()print fileContents[-1] # This is the last line. On 10/19/06, Asrarahmed Kadri <[EMAIL PROTECTED]> wrote: My algorithm is like this: first count

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Asrarahmed Kadri
My algorithm is like this: first count the number of lines in the file by using a loop. Use a second loop and when teh counter reaches the num_of_lines values: take the line.   Is there any other way to do it??   On 10/19/06, Danny Yoo <[EMAIL PROTECTED]> wrote: On Thu, 19 Oct 2006, Asrarahmed Kadr

Re: [Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Danny Yoo
On Thu, 19 Oct 2006, Asrarahmed Kadri wrote: > I want to extract the last line of the text file. Any idea ??? Hi Asrarahmed, Ok, so what part do you get stuck on when you try to do this? That is, what sort of things do you already know how to do with files? __

[Tutor] Trying to extract the last line of a text file

2006-10-19 Thread Asrarahmed Kadri
  Hi Folks,   I want to extract the last line of the text file. Any idea ???   Thanks, Regards, Asrarahmed-- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor