# 指定要处理的文件名
input_filename = '中文目录.txt'
output_filename = 'output_3.txt'# 定义要排除的字符列表
exclude_characters = ['参考文献', '习题', '进一步阅读']# 打开原始文件和新文件
with open(input_filename, 'r', encoding='utf-8') as file:lines = file.readlines()# 打开新文件准备写入
with open(output_filename, 'w', encoding='utf-8') as file:for line in lines:# 检查当前行是否包含指定内容if not any(char in line for char in exclude_characters):file.write(line)# 如果需要,可以用新文件替换原始文件
# import os
# os.remove(input_filename)
# os.rename(output_filename, input_filename)