Changing a line in a text file

Kirk Job Sluder kirk at jobsluder.net
Mon Apr 25 13:39:36 EDT 2005


Steve Holden <steve at holdenweb.com> writes:

> kah wrote:
> However, you asked about replacing one line with another of a
> different length: since this will mean changing the offsets of all
> subsequent bytes you have no way to do this other than writing out the
> whole content of the file following the modification. You would also
> have to ensure that you truncated the file to the correct length.
> 
> In general, although they don't make it obvious that they are doing so
> most programs that "change" files (text editors and the like) are
> really writing new copies.

In addition, I would argue that editing a file in place using a
non-interactive program is dangerous and bad practice in general.  By
the time you find a bug in your edit script, the original is lost.  This
is something I learned from bitter experience when I tried to be smart
and make script-based edits over entire directories of html files.

In unix shell scripting idiom, I would do something like:

  mv file file.bak
  sed -e 'g/oldline/c newline' < file.bak > file

And yes, I know that some versions of sed have the --in-place option.

Then, I would check for side effects:

  diff file file.bak

All of this can be done in python, however I'm not overly familiar with
difflib and it seems to require both versions of the file in memory.  So
an external diff might be better.

import os
os.rename(foo,foo.bak)
infile = open(foo.bak,'r')
outfile = open(foo,'w')
for line infile:
   #test and substitution code block
   outfile.write(line)

Using separate input and output files also has the advantage of being
memory efficient.  
    


> 
> regards
>   Steve
> -- 
> Steve Holden        +1 703 861 4237  +1 800 494 3119
> Holden Web LLC             http://www.holdenweb.com/
> Python Web Programming  http://pydish.holdenweb.com/
> 

-- 
Kirk Job-Sluder
"The square-jawed homunculi of Tommy Hilfinger ads make every day an
existential holocaust."  --Scary Go Round



More information about the Python-list mailing list