(Dec-14-2021, 12:36 AM)greatfella Wrote: I tried many iterations, wish I could remember all of them to post. I just tried f.close() at the end and it still didn't work.It will not work as you try to do
inplace
of original file.Look Gribouillis code it work as he use fileinput with
inplace=True
.(Dec-14-2021, 12:36 AM)greatfella Wrote: i am trying to replace the entire line as in replace "whiffenhard" with "woo"Output of Grib code in
fn.txt
:Output:yipee
wahooo
woo
yerp
To get same output with menator01 code change line 9.new_line = re.sub('whiffenhard', 'woo', line)He dos a extra step make new list then overwrite original file,then avoid the
inplace
problem. A third party module in-place
from in_place import InPlace. with InPlace('fn.txt') as fp: for line in fp: line = line.replace('whiffenhard', 'woo') fp.write(line)
Output:yipee
wahooo
woo
yerp