[Tutor] How to delete the last line from a file efficiently?

Christian Witts cwitts at compuscan.co.za
Thu Feb 18 11:49:17 CET 2010


Joson wrote:
> Hi all,
>     Now I have a text file about 1MB.  Sometimes I need to remove the 
> last line.
>     The current method I use is reading all content, finding the last 
> sentence, remove it and write the new content to the file.
>     So, it need to read a 1MB file and write a 1MB file. It's 
> ineffictient, I think. Are there any other methods?
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>   
Take a look at file.seek to move to a specific section of the file and 
file.truncate to truncate a file at a specific size, rough 
implementation would be something like.

BUFFER = 2**10  # Set your buffer size
f = open(filename, 'r')
multiple = -1
found_it = False

while not found_it:
    f.seek(BUFFER * multiple, os.SEEK_END)
    # Seeks from the file endpoint the specified by your buffer * 
'iteration' of the app
    offset = f.tell()       # gets the current position
    data = f.read(BUFFER)   # read in your data and process it
    # perform operations to get your termination point offset
    if termination_point_not_found:
        multiple -= 1
    else:
        # once you find your position to end the file on truncate the 
file and exit loop
        f.truncate(offset + termination_point_offset)
        found_it = True

f.close()

It's untested code, but it should work just fine.
As for how fast it is by comparison, I unfortunately don't have time to 
benchmark it.

-- 
Kind Regards,
Christian Witts




More information about the Tutor mailing list