发布作者: Southerly
最后更新: 2023年 10月 18日 15:06
作品采用: 《 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 》许可协议授权
使用说明:
1.需要有python环境
2.将脚本放在需要处理的文件夹里,双击运行就可以了
源码如下:
import os
# 获取当前目录下所有txt文件
def get_txt_files():
files = []
for file in os.listdir("."):
if file.endswith(".txt"):
files.append(file)
return files
# 比较所有txt文件并删除相同的文件
def compare_and_delete():
while True:
txt_files = get_txt_files()
deleted_files = set() # 存储被删除的文件
for i in range(len(txt_files)):
if txt_files[i] in deleted_files:
continue
for j in range(i + 1, len(txt_files)):
if txt_files[j] in deleted_files:
continue
file1 = txt_files[i]
file2 = txt_files[j]
with open(file1, "r") as f1, open(file2, "r") as f2:
if f1.read() == f2.read():
os.remove(file2)
print("删除文件:", file2)
print("相同的文件:", file1, file2)
deleted_files.add(file2)
if not deleted_files: # 如果没有要删除的文件,则跳出循环
break
# 执行脚本
compare_and_delete()
# 在处理完成后停留在命令窗口
raw_input() # For Python 2
# input() # For Python 3
本文共 109 个字数,平均阅读时长 ≈ 1分钟
请勿在本站点发表不当言论,以及进行恶意引流行为,否则会对该ip进行封禁处理
学习学习!
OωO