文件排重

2017-12-01  本文已影响10人  blensmile
import os
import re

def _withPopen(commands):
    return os.popen(commands)

def _getMD5(string):
    items = string.split("\n")
    dictionary = {}
    repeats = set()
    patter = ".*\((.*)\) = (\\w+)"
    reg = re.compile(patter)
    for item in items:
        if(reg.match(item)):
            key = reg.sub("\\1",item)
            value = reg.sub("\\2",item)
            if(dictionary.has_key(value)):
                repeats.add(value)
            dictionary.setdefault(value,[]).append(key)
    rep = {}
    for item in repeats:
        rep[item]=dictionary[item]
    return rep


path = "input your path" 
command = "ls -psS "+path + " | grep  \"^\ *[1-9]\""
results = _withPopen(command).read()
items = results.split("\n")
dictionary = {}
repeats = set()
for item in items:
    if(re.match(" *\\d+ .+",item)):
        s = item.strip().split(" ",1)
        value = s[0]
        key = s[1]
        if(dictionary.has_key(value)):
            repeats.add(value)
        dictionary.setdefault(value,[]).append(key)
aaa = []
for key,value in dictionary.items():
    if(len(value)>1):
        aaa.append(value)
for item in aaa:
    string = ""
    for i in item:
        string += "\""+path+i+"\" "
    result = os.popen("md5 "+string).read()
    print _getMD5(result) 
上一篇 下一篇

猜你喜欢

热点阅读