| Forum Home | ||||
| Press F1 | ||||
| Thread ID: 46626 | 2004-06-30 09:31:00 | Python : How to access the last record in a file | andrew93 (249) | Press F1 |
| Post ID | Timestamp | Content | User | ||
| 248621 | 2004-06-30 09:31:00 | Hi, 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 python.org, 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 |
andrew93 (249) | ||
| 248622 | 2004-07-01 03:30:00 | 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 (docs.python.org). | Graham L (2) | ||
| 1 | |||||