Best way to get Last line of file? 
Author Message
 Best way to get Last line of file?

It there a better way to get the last line of a file.
And is there a better way to get the date it the format 09-Jan-1997?
Thanks for any suggestions on how to improve this code.

import string
import time
filename='myfile.txt'
try:
  input = open(filename,'r')
except:
  sys.stderr.write('Error opening %s !\n' % filename)
  sys.exit(2)
while 1:
  line = input.readline()
  if not line:
    break
  lline = line

tstring = time.ctime(time.time())
if ( tstring[8:9] == ' '):
  tstring = "0" + tstring[9:10] + '-' + tstring[4:7] + '-' + tstring[-4:]
else:
  tstring = tstring[8:10] + '-' + tstring[4:7] + '-' + tstring[-4:]



Thu, 01 Jul 1999 03:00:00 GMT  
 Best way to get Last line of file?

    Jerry> It there a better way to get the last line of a file.

I believe there is a seek method for files.  Check the library docs.  You
could seek to near the end of the file, call read() to get the rest, split
on newlines (or CRLF or CR) and pick the last line out of the list.  If that
fails to give more than one line, back up the seek aways and repeat.

    Jerry> And is there a better way to get the date it the format 09-Jan-1997?

Check out the strftime function of the time module.

--
Skip Montanaro     |       Musi-Cal:  http://concerts.calendar.com/

(518)372-5583      |    long as you eat at home." -- Sloan Wainwright



Thu, 01 Jul 1999 03:00:00 GMT  
 Best way to get Last line of file?


Quote:
>It there a better way to get the last line of a file.

import string
fileName = 'myfile.txt'

string.split(open(fileName, 'r').read(), '\n')[-1]

if you're on a Unix system use '\012' instead of '\n'

Quote:
>And is there a better way to get the date it the format 09-Jan-1997?

import time, string

theTime = string.split(time.ctime(time.time()))
theTime = theTime[2] + '-' + theTime[1] + '-' + theTime[-1]

Steve




Fri, 02 Jul 1999 03:00:00 GMT  
 Best way to get Last line of file?

Quote:

> string.split(open(fileName, 'r').read(), '\n')[-1]

> if you're on a Unix system use '\012' instead of '\n'

'\n' works for me, on a HP-UX system...

--

Bailey, Colorado



Fri, 02 Jul 1999 03:00:00 GMT  
 Best way to get Last line of file?

Quote:

> > string.split(open(fileName, 'r').read(), '\n')[-1]

> > if you're on a Unix system use '\012' instead of '\n'

> '\n' works for me, on a HP-UX system...

If you open a file in default mode ("r" or no flag at all, note!), its
opened in text mode on platforms that bother, which means that native
newline is mapped to "\n".  So splitting on "\n" is ok on all
platforms.  If you open in binary mode ("rb"), you have to use "\r\n"
under DOS, "\r" on a Mac (?), and "\n" on Unix.

And yes, "\012" is exactly the same thing as "\n", independent of
platform:

        >>> "\n"
        '\012'

Cheers  /F



Fri, 02 Jul 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Getting a file's last modified time

2. read the last line in file

3. join last 2 lines of a file

4. deleting newline from last line in file

5. print last line of input file

6. how to specify last line of file ?

7. How to read the x last lines of a text file

8. How to read the last line of http:// file

9. File: How to read the last line?

10. last 20 lines of a file on WinDoze ?

11. Deleting the last lines in a text file with tcl

12. Removing last line while appending to file

 

 
Powered by phpBB® Forum Software