Flatfile update?

Alex Martelli aleax at aleax.it
Sun Sep 16 15:36:37 CEST 2001


pwgk wrote:

> Hi,
> I am trying to find out if it is possible to open a flat (ascii) file for
> update in python, so I can change it's contents directly, without having
> to copy the entire contents.

Yes, the "r+" option to the built-in open function lets you open
an existing file so you can both read and write its data.  However,
on just about all of today's operating systems/filesystems, any
bytes you should write at a given offset are just overwriting the
bytes that were there before; so, in practice, this is only useful
if each field you may want to overwrite is of fixed length.  There
is no way to "insert" bytes in-between ones that were there, or
"remove" bytes from somewhere in the middle of the file (as
opposed to overwriting those bytes with others).

> Is this possible, and if yes, does anyone know how?
> Or do I have to start digging into MySQL for that?

Any RDBMS, and some lighter-footprint alternatives such as
the Berkeley DB (which Python lets you access easily) will no
doubt provide a vastly more flexible alternative.  However, they
do not meet your stated specs of "changing an ascii's file's
contents directly" -- they operate in completely different ways.


Alex




More information about the Python-list mailing list