Remove lines that contain certain string Remove lines that contain certain string python python

Remove lines that contain certain string


You can make your code simpler and more readable like this

bad_words = ['bad', 'naughty']with open('oldfile.txt') as oldfile, open('newfile.txt', 'w') as newfile:    for line in oldfile:        if not any(bad_word in line for bad_word in bad_words):            newfile.write(line)

using a Context Manager and any.


I have used this to remove unwanted words from text files:

bad_words = ['abc', 'def', 'ghi', 'jkl']with open('List of words.txt') as badfile, open('Clean list of words.txt', 'w') as cleanfile:    for line in badfile:        clean = True        for word in bad_words:            if word in line:                clean = False        if clean == True:            cleanfile.write(line)

Or to do the same for all files in a directory:

import osbad_words = ['abc', 'def', 'ghi', 'jkl']for root, dirs, files in os.walk(".", topdown = True):    for file in files:        if '.txt' in file:            with open(file) as filename, open('clean '+file, 'w') as cleanfile:                for line in filename:                    clean = True                    for word in bad_words:                        if word in line:                            clean = False                    if clean == True:                        cleanfile.write(line)

I'm sure there must be a more elegant way to do it, but this did what I wanted it to.


You could simply not include the line into the new file instead of doing replace.

for line in infile :     if 'bad' not in line and 'naughty' not in line:            newopen.write(line)