Books Database

Alex Martelli aleaxit at yahoo.com
Fri Mar 7 09:03:15 EST 2003


On Friday 07 March 2003 02:09 pm, Eliran Gonen wrote:
> Alex Martelli <aleaxit at yahoo.com>:
> >     thefile = open('X','r+')
> >     lines = thefile.readlines()
> >     del lines[X]
> >     thefile.seek(0)
> >     thefile.writelines(lines)
> >     thefile.close()
>
> The code is:
>
> infile = open(database, 'r+')
> lines = infile.readlines()
> del lines[0]
> infile.seek(0)
> infile.writelines(lines)
> infile.close()
>
> Now the database is like that :
>
> sample|none|1924|nobody
> stuff|two|1934|tree
>
> Now executing the above code results in:
>
> stuff|two|1934|tree
> ody
> stuff|two|1934|tree
>
> Bizarre.

Ah, yes, sorry -- you need to add a call to thefile.truncate() right before
you close the file, to ensure the file on disk reflects any shortening that
may have happened since the last write.


Alex






More information about the Python-list mailing list