"Inserting" a line in a text file?
Jeff Shannon
jeff at ccvcorp.com
Wed May 8 14:56:04 EDT 2002
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'd suggest using a database like MySQL to prevent the need to
insert at the beginning of a flat file, but.... >ahem< ;)
--
Jeff Shannon
Technician/Programmer
Credit International
More information about the Python-list
mailing list