首页 >>  正文

p+在python中表示什么

来源:baiyundou.net   日期:2024-09-21

作者:siseniao

从该文章改进:https://post.smzdm.com/p/an370emp/?zdm_ss=Android_1106136211_&send_by=1106136211&from=other&invite_code=zdmwffzv7winv

原文每次扫描都需要重新计算MD5,对于大文件来说,磁盘消耗较大,增加了缓存文件存储md5,每次扫描只计算新文件,提高效率。

不废话,直接贴代码:

import os

import hashlib

# 只删除以下列表中的重复文件类型.如果想删除其他类型的文件,自己添加一下就行了

file_type = ['.jpg', '.jpeg', '.png', '.gif', '.psd', '.bmp', '.webp', '.mp4', '.mkv', '.avi', '.mov', 'mpeg', 'mpg',
            '.rar', '.zip']
check_files = []

#自行修改目录列表
work_dir_list = [r'/volume2/111', r'/volume1/222']


def save_md5_file(files_dict:dict):
   if files_dict is None:
       return
   try:
       with open("md5.txt", "w") as f:
           for path_md5, file_md5, in files_dict.items():
               f.write(str(path_md5) + "=" + str(path_md5) + 'n')

   except Exception as e:
       pass


def open_md5_file():
   files_md5 = {}
   try:
       with open("md5.txt", "r") as f:
           for md5_line in iter(lambda: f.readline(), ""):
               list_keys = md5_line.split('=')
               if len(list_keys) == 2:
                   files_md5[list_keys[0].strip()] = list_keys[1].strip()
   except Exception as e:
       pass

   return files_md5


def remove_repeat_files():
   for work_dir in work_dir_list:
       for root, dirs, files in os.walk(work_dir):
           for name in files:
               p_type = os.path.splitext(os.path.join(root, name))[1]
               if p_type in file_type:
                   check_files.append(os.path.join(root, name))
           for name in dirs:
               p_type = os.path.splitext(os.path.join(root, name))[1]
               if p_type in file_type:
                   check_files.append(os.path.join(root, name))
   files_dict = {}
   files_md5 = open_md5_file()
   r_index = 0
   print('Files Num:%s' % len(check_files))

   for file_path in check_files:
       try:
           md5_path = hashlib.md5()
           md5_path.update(file_path.encode('utf-8'))
           path_md5 = md5_path.hexdigest()
           file_md5 = files_md5.get(path_md5)
           if file_md5 is None:
               md5_hash = hashlib.md5()
               with open(file_path, "rb+") as f:
                   for byte_block in iter(lambda: f.read(4096), b""):
                       md5_hash.update(byte_block)
                   file_md5 = md5_hash.hexdigest()
               print('Check file MD5:%s' % file_path)
               files_md5[path_md5] = file_md5

           if files_dict.get(file_md5) is None:
               files_dict[file_md5] = file_path
           else:
               d_path = files_dict[file_md5]
               d_path_stats = os.stat(d_path)
               file_stats = os.stat(file_path)
               d_time = d_path_stats.st_ctime
               f_time = file_stats.st_ctime
               if d_time > f_time:
                   os.remove(d_path)
                   files_dict[file_md5] = file_path
                   print('Delete File:', d_path)
                   r_index += 1
               else:
                   os.remove(file_path)
                   print('Delete File:', file_path)
                   r_index += 1
       except Exception as e:
           pass

   print('File Count:%s, Repeat Files Num:%s. All deleted!' %( len(check_files),str(r_index)))
   save_md5_file(files_md5)


if __name__ == '__main__':
   remove_repeat_files()
   


可以在ssh或者任务计划里执行

","gnid":"9a931522e9730c14b","img_data":[{"flag":2,"img":[{"desc":"","height":"385","title":"","url":"https://p0.ssl.img.360kuai.com/t01a2508b2adc68479c.jpg","width":"600"}]}],"original":0,"pat":"art_src_1,fts0,sts0","powerby":"cache","pub_time":1679316661000,"pure":"","rawurl":"http://zm.news.so.com/1715a84bea2900132874605fea6f9a81","redirect":0,"rptid":"71260418b6e0ce01","rss_ext":[],"s":"t","src":"什么值得买","tag":[],"title":"利用python删除群晖重复文件(缓存文件MD5方式)

姬艺鸿932python +=表示什么?
房中桦13589047887 ______ 如下: year += 1 等同于 year = year + 1

姬艺鸿932""" """在python中是什么意思 -
房中桦13589047887 ______ 一般在格式化的时候会用到%.这里你用的 %Y表示年,%m表示月,%d表示日 %H表示时,%M表示分,%S表示秒 单独用%号的时候,实际上没什么意义.

(编辑:自媒体)
关于我们 | 客户服务 | 服务条款 | 联系我们 | 免责声明 | 网站地图 @ 白云都 2024