# @Author : uccoa
import hashlib import os def getFilesByDir(target_path, include_str=None, filter_strs=None): """ 获取指定目录下所有的文件(不包含以__开头和结尾的文件)或指定格式的文件,若不同目录存在相同文件名,只返回第1个文件的路径 :param target_path: 查找的目录路径 :param include_str: 获取包含字符串的名称,不区分大小写 :param filter_strs: 过滤包含字符串的名称,区分大小写 """ if filter_strs is None: filter_strs = [] files = [] names = os.listdir(target_path) for name in names: path = os.path.abspath(os.path.join(target_path, name)) if os.path.isfile(path): # 寻找包含指定字符串的文件名 if include_str is not None and include_str.upper() not in name.upper(): continue # 寻找不包含filter_strs中的字符 for filter_str in filter_strs: if filter_str in name: break else: files.append(path) #递归获取子文件夹里的文件 # else: files += getFilesByDir(path, include_str=include_str, filter_strs=filter_strs) return files def getSHA1ListByFiles(files): """ 获取所有文件的115sha1值 :param files: 各文件的绝对路径 """ sha1List = [] for targetFile in files: with open(targetFile, 'rb') as f: sha1 = hashlib.sha1() firstBlock = f.read(128 * 1024) sha1.update(firstBlock) #第二个sha1值 hash2 = sha1.hexdigest().upper() secondBlock = f.read() sha1.update(secondBlock) #第一个sha1值 hash1 = sha1.hexdigest().upper() #文件大小 fileSize = str(len(firstBlock + secondBlock)) sha1List.append('115://' + str(targetFile[targetFile.rindex('\\')+1:]) + '|' + fileSize + '|' + hash1 + '|' + hash2) return sha1List target_path = 'D:/115down/' files = getFilesByDir(target_path, '') SHA1List = getSHA1ListByFiles(files) for sha1 in SHA1List: print(sha1)