# -*- coding: utf-8 -*-
# @Time : 2018/12/5 10:21
# @Author : Monica
# @Email : 498194410@qq.com
# @File : os_practice.py
import os
import hashlib
allfiles = []
templist = {}
end_file = []
class GetFlie:
def __init__(self, dirpath):
self.dirpath = dirpath
def get_all_file(self):
# 通过os.walk获取目录下的所有文件名
for root, dirs, files in os.walk(self.dirpath):
for file in files:
# 判断size
size = os.path.getsize(os.path.join(root, file))
if size >= 10485760:
# 文件名添加到allfiles列表里
allfiles.append(os.path.join(root, file))
# 重命名
for i in range(len(allfiles)):
# 如果md5在字典的key已存在
if self.get_md5(allfiles[i]) in templist.keys():
templist[self.get_md5(allfiles[i]) + str(i)] = allfiles[i].split(".")[0] + str(i) + "." + allfiles[i].split(".")[-1]
else:
templist[self.get_md5(allfiles[i])] = allfiles[i]
# 最后的文件
for file in templist.values():
end_file.append(file)
return end_file
@staticmethod
def get_md5(filename):
f = open(filename, 'rb')
m1 = hashlib.md5()
m1.update(f.read())
hash_code = m1.hexdigest()
f.close()
md5 = str(hash_code).lower()
return md5
if __name__ == '__main__':
path = r'I:\lesson_practice\tool'
print(GetFlie(path).get_all_file())