Can I change one line in a file without rewriting the whole thing?

David Wahler dwahler at gmail.com
Sat Jul 14 00:34:51 EDT 2007


On 7/13/07, J. J. Ramsey <jjramsey at pobox.com> wrote:
> In Perl, there is a module called "Tie::File". What it does is tie a
> list to each line of a file. Change the list, and the file is
> automatically changed, and on top of this, only the bits of the file
> that need to be changed are written to disk. At least, that's the
> general idea.
>
> I was wondering if something roughly similar could be done in Python,
> or at the very least, if I can avoid doing what amounts to reading the
> whole file into memory, changing the copy in memory, and writing it
> all out again.

The mechanism behind Perl's ties -- an array that, when read from or
written to, passes control to a user function -- is easy to implement
in Python. See the documentation of the mapping protocol
<http://docs.python.org/ref/sequence-types.html>:

>>> class SpecialList(object):
...     def __getitem__(self, index):
...        return "Line %d" % index
>>> foo = SpecialList()
>>> foo[42]
'Line 42'

>From the documentation for Tie::File, it doesn't look like a trivial
piece of code; for example, it has to maintain a table in memory
containing the offset of each newline character it's seen for fast
seeking, and it has to handle moving large chunks of the file if the
length of a line changes. All this could be implemented in Python, but
I don't know of a ready-made version off the top of my head.

If all you want is to read the file line-by-line without having the
whole thing in memory at once, you can do "



More information about the Python-list mailing list