replace a line in a text file

Sergey Krushinsky skru at
Fri Jun 11 17:09:27 CEST 2004

Larry Bates wrote:

>2) File isn't tremendously big, if it is you should
>use loop and xreadlines() to process one line at a
Would you suggest using regular expressions in case of a large file? The 
first idea which came to my mind when I saw the Luis's question was:

import sys
import re


p = re.compile('^(.*?4\.5.*?)$', re.MULTILINE)
f = open(_FILENAME, 'r')
old_txt =
new_txt = p.sub('', old_txt)
open(_FILENAME, 'w').write(new_txt)

With best regards,

More information about the Python-list mailing list