閱讀407 返回首頁    go 微軟 go windows


python3 文件去重

import hashlib
import os


class DirProc():
    def __init__(self):
        self.main_dir = "C://software//導數據//mtm//imgs"

    def md5(self, fname):
        if os.path.exists(fname):
            hash_md5 = hashlib.md5()
            with open(fname, "rb") as f:
                for chunk in iter(lambda: f.read(4096), b""):
                    hash_md5.update(chunk)
            return hash_md5.hexdigest()

    def remove(self, fpath1, fpath2):
        md51 = self.md5(fpath1)
        md52 = self.md5(fpath2)
        if md51 == md52:
            os.remove(fpath1)
            print("去重", fpath1)

    def remove_duplicate(self):
        dir_list = os.listdir(self.main_dir)
        for name in dir_list:
            sub_dir = self.main_dir + "//" + name;
            file_list = os.listdir(sub_dir)
            for fname in file_list:
                for sub_fname in file_list:
                    if fname == sub_fname:
                        continue
                    f_dir = sub_dir + "//" + fname;
                    f_sub_dir = sub_dir + "//" + sub_fname;
                    self.remove(f_dir, f_sub_dir)


dirProc = DirProc()
dirProc.remove_duplicate()

最後更新:2017-07-04 11:32:51

  上一篇:go  快速入門:10分鍾全麵了解持續交付平台雲效
  下一篇:go  java 網站站源碼 在線編輯模版 代碼編輯器 兼容手機平板PC freemaker 靜態引擎