Removing last line while appending to file

Tim Wegener twegener at
Wed Apr 30 09:13:23 CEST 2003

Someone emailed me directly with the solution: use open(filename, "r+")
This solved my problem. 
(I was using the wrong mode for open())


On Wed, 30 Apr 2003 15:26:08 +0930
Tim Wegener <twegener at> wrote:

> Hi, 
> Is there a simple way to remove the last line (or arbitrary trailing
> portion) of a file, when appending to it? 
> I want to write a log file in xml, which requires root elements around
> all the log data. This means that when I want to append to the log I
> must first remove the trailing </log> element. 
> I have tried using and file.tell() to no avail. I would
> prefer not to parse the entire file each time I want to simply append an
> entry. 
> Thanks, 
> Tim

RADLogic Pty Ltd                Phone: +61-8-8362-3255
Suite 11, 15 Fullarton Road     Fax:   +61-8-8362-3166
Kent Town                       Email:  twegener at
South AUSTRALIA, 5067           WWW:

More information about the Python-list mailing list