希望你每天醒來都是陽光的,不會因爲別人的幾句話,幾個表情和幾個舉止影響自己的心情,好好生活,總會遇見美好的事。。。
---- 網易雲熱評
一、下載地址
https://github.com/teamssix/quchong
二、下載pyhon腳本到本地
git clone https://github.com/teamssix/quchong.git
三、用法
1、必須python2環境
2、把去重的文件和該python腳本放到一起
3、新建幾個有重複內容的文件,放到一個與python腳本不在一起的位置/root/123
4、修改python腳本
#coding=utf-8
import sys, re, os
def file_merge():
input_path = "/root/123/" #此處填好自己的路徑,注意最後的"/"
#使用os.listdir函數獲取路徑下的所有的文件名,並存在一個list中
#使用os.path.join函數,將文件名和路徑拼成絕對路徑
whole_file = [os.path.join(input_path,file) for file in os.listdir(input_path)]
content = []
#對於每一個路徑,將其打開之後,使用readlines獲取全部內容
for w in whole_file:
with open(w,'rb') as f:
content = content+f.readlines()
#構造輸出的路徑,和輸入路徑在同一個文件夾下,如果該文件夾內沒有這個文件會自動創建
output_path = os.path.join(input_path,'合併所有文件.txt')
#將內容寫入文件
with open(output_path,'wb') as f:
f.writelines(content)
def getDictList(dict):
regx = '''[\w\~`\!\@\#\$\%\^\&\*\(\)\_\-\+\=\[\]\{\}\:\;\,\.\/\<\>\?]+'''
with open(dict) as f:
data = f.read()
return re.findall(regx, data)
def rmdp(dictList):
return list(set(dictList))
def fileSave(dictRmdp, out):
with open(out, 'a') as f:
for line in dictRmdp:
f.write(line + '\n')
def main():
try:
dict = '/root/123/合併所有文件.txt'
out = '/root/123/去重所有文件.txt'
except Exception, e:
print 'error:', e
me = os.path.basename(__file__)
exit()
dictList = getDictList(dict)
dictRmdp = rmdp(dictList)
fileSave(dictRmdp, out)
if __name__ == '__main__':
file_merge()
main()
5、python2 quchong.py運行腳本,會在/root/123目錄下生成去重文件
6、工作原理
將當前目錄下所有文件合併爲一個文件:合併所有文件.txt,對該文件進行去重,最終生成去重所有文件.txt,該文件就是最後想要的文件。