Results 1 to 2 of 2
  1. #1

    Default Python : How to access the last record in a file


    Thanks to the people who helped me previously with my Python question. Also, speaking as a Python newbie, what a great product! It is rip-snortingly fast and what took me a year or more on Access, I have now achieved on Python in 2 weeks - plus the data handling rate is about a squillion times faster so I am now up to 30m records (in 24 hours, and still counting) where I was up to 100k on Access after a long long time, but...

    I have struck a minor problem. I have created a programme that is handling large volumes of numbers within text files (FYI, I convert the string to an integer using the int function prior to handling the number - I couldn't find a way of storing integers in a file other than a text file). However, at the very start of my programme I need to access the last record in a given file (for arguments sake lets call it "number.txt" which has millions of records in it), in order to calculate an upper limit. I know how to load the file but how do I then access the last record? Using the following command only stores the first record from the file (for arguments sake) in the variable "test" :

    test = int(number.readline())

    I have tried using a -1 inside the brackets, to no avail. Is there a quick shortcut I can use to do this?

    Given the size of the file (millions of records), I don't want to use the readlines() function to read all of the records and then look at the last one - it is slowing the process down. And I suspect that assigning all of the values to an array will be equally inefficient (but correct me if I am wrong).

    So far I have looked at the documentation and FAQ at, I have tried Google and looked at the online help within Python. None of these seem to have the answer.

    Does anyone in F1 know how to open a file in Python and jump to the last record?

    FYI I am running Python version 2.3 & XP Pro

  2. #2
    Graham L

    Default Re: Python : How to access the last record in a file

    Have you tried the seek(offset, whence) command? If you have opened the file in "append" ('a') mode, it's really a no-op, but if you are in "append with read" ('a+') it could be useful. A seek(-1,2) then a read() looks as it should get you the record. Have a look at the Builtin file objects.

Similar Threads

  1. Python Script
    By Faded_Mantis in forum PressF1
    Replies: 1
    Last Post: 18-02-2008, 07:14 AM
  2. Looping in Python
    By Mike in forum PressF1
    Replies: 15
    Last Post: 27-06-2006, 09:03 PM
  3. Replies: 11
    Last Post: 09-06-2004, 11:44 AM
  4. File Access Permissions
    By tkelly in forum PressF1
    Replies: 11
    Last Post: 14-05-2004, 03:43 PM
  5. Python
    By jonzsie in forum PressF1
    Replies: 4
    Last Post: 21-02-2003, 10:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts