"Inserting" a line in a text file?

Peter Hansen peter at engcorp.com
Thu May 9 00:50:56 EDT 2002


Jeff Shannon wrote:
> 
> In article <ab9gnu$g5muf$1 at ID-99293.news.dfncis.de>, William Park
> says...
> >
> > > On Tue, May 07, 2002 at 09:34:02PM +0100, Duncan Smith wrote:
> > >> I need to (in effect) insert a single line at the beginning of an
> > >> existing text file (a dumped MySQL database, so potentially large).  Is
> > >> there anything cuter than  eg. creating a temporary file containing the
> > >> single line, reading the lines from the data file, appending them to the
> > >> temporary file, deleting the data file, renaming the temporary file?
> >
> > If you have access to 'ed' or 'vi', then you can insert using the text
> > editors.
> 
> Yes, but internally, those editors are doing the exact same thing
> that he's hoping to avoid -- reading the file into memory and
> writing it back out with the extra line inserted.  Really,
> there's no practical way to avoid this -- it's just the way that
> filesystems work.  You can minimize the damage by reading in
> smallish chunks at a time (say, 8k) but you *will* have to spin
> through the entire file.

I wonder whether a journalling/logging file system would support
an insertion operation *without* the need to rewrite the whole file.
I know you can overwrite portions of the file without touching
the current content (it appends an operation recording that a range
has been overwritten) and you can append easily, but do they
understand insertion?

(Not that this in any way even remotely helps the OP... just curious.)

-Peter



More information about the Python-list mailing list