# coding=utf-8
#Python 读取某个目录下所有的文件实例
import os
import os.path
import re
import sys
import codecs
reload(sys)
sys.setdefaultencoding('utf-8')
path = 'D:/mylinux/Git/mxnet-ssd/result'
files = os.listdir(path.decode('utf-8'))
#用set可以很好的去重
datas = set()
for file in files :
txt_path = 'D:/mylinux/Git/mxnet-ssd/result/'+file.decode('utf-8')
#把结果保存了在contents中
contents = codecs.open(txt_path.decode('utf-8'),'r',encoding='utf-8')
datas.clear()
#把数据add到datas中,可以去重
for content in contents:
print(content.decode('utf-8'))
datas.add(content.decode('utf-8'))
#去重后新的文件保存的路径
new_txt_path = 'D:/mylinux/Git/mxnet-ssd/result/' + file.decode('utf-8')
unique_keywords = codecs.open(new_txt_path.decode('utf-8'), 'w', encoding='utf-8')
#把datas里的数据输出到新生成的txt中
for data in datas:
unique_keywords.write(data+"\n")
unique_keywords.close()
From:https://www.jb51.net/article/142504.htm