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
?
How come, this has removed
blih
orbluh
?
To answer your question, you truncate the file too often. Every call to f.truncate(0)
will reset the file size to 0. Therefore only the last line blihihih
will survive.
If you want to do it in one go:
f = open('test.txt', 'r+')
lines = f.readlines()
f.seek(0)
for line in lines:
if line.find('blah') < 0:
f.write(line)
f.truncate()
f.close()
Additionally, you should really use with
:
with open('test.txt', 'r+') as f:
lines = f.readlines()
f.seek(0)
for line in lines:
if line.find('blah') < 0:
f.write(line)
f.truncate()
(You won't need to write f.close()
then. Read more about why using with
here for example.)
I think is best if you open the file first, and then write to it:
# open file
lines = open('test').readlines()
# over-write the file
with open('test', 'w') as out:
for line in lines:
if 'blah' not in line:
out.write(line)
Output (in test)
bluh
blih
blihihih
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.