I have one column list file:
$ cat test
blah
blahblah
blah22
bluh
blih
blihihih
All I want to is to remove blah
-like lines. (Equivalent of bash grep -v blah
.) So that blah
, blahblah
and blah22
are removed.
I have this so far:
>>> f = open('test', 'rw+')
>>> line = f.readlines()
>>> for l in line:
... if l.find('blah') == -1:
... f.truncate(0)
... f.write(l)
...
>>> f.close()
I thought it would be fine, but after running this, from test file only this line is left:
$cat test
blihihih
How come, this has removed blih
or bluh
?