I have a text file with multiple lines of text. What I would like to do is to search that file for a specific word and if that line does contain that word, how do I delete that line only. (Python)
Asked
Active
Viewed 476 times
-1
-
related: [Is it possible to modify lines in a file in-place?](http://stackoverflow.com/q/5453267/4279) – jfs Mar 22 '15 at 10:00
1 Answers
0
To remove lines that contain a word in the files given on the command-line:
#!/usr/bin/env python3
import fileinput
word = 'put your word here'
for line in fileinput.input(inplace=True):
if word not in line:
print(line, end='') # keep the line

jfs
- 399,953
- 195
- 994
- 1,670