您可以像这样使代码更简单,更易读
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)使用上下文管理器和任何。

您可以像这样使代码更简单,更易读
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)使用上下文管理器和任何。