import os
# 指定要遍历的文件夹路径
folder_path = 'E:\\Users\091\\Desktop\\工具箱\月26日御剑1.5\\御剑配置文件'
# 指定输出的文件路径
output_file_path = 'E:\\Users\091\\Desktop\\工具箱\月26日御剑1.5\\御剑配置文件\\output.txt'
# 初始化一个空来存储不重复的行
unique_lines = set()
# 遍历文件夹下的所有文件
for filename in os.listdir(folder_path):
if filename.endswith('.txt'): # 只处理文本文件
with open(os.path.join(folder_path, filename), 'r') as f:
for line in f:
unique_lines.add(line.strip()) # 去除末尾换行符,并添加到中(自动去重)
# 将不重复的行写入新的文本文件
with open(output_file_path, 'w') as outfile:
for line in sorted(unique_lines): # 可选:按字典顺序排序
outfile.write(line + '\n') # 添加回换行符并写入文件
print("All unique lines have been written to the file: ", output_file_path)
还得说学好python办公是真的舒服,目前这个脚本存在的一些缺陷,是它只能对文本文件进行去重,如果你是Word文档啊什么的,不行,等我再研究研究怎么对Word文件进行批量去重。