黑群晖利用存储空间分析器和python脚本,删除重复的文件
由于自己瞎鼓捣Mentens,导致Drive里的重复照片和视频越来越多了.于是便使用存储空间分析器,看一下有哪些重复的照片和视频(存储空间分析器安装很简单,打开套件中心搜索 存储空间分析器,按就行了).
多图预警
分析是分析完了.也可以在分析器里手动删除(你要真这样删除,我也无话可说.毕竟我又不是二傻子......
)
Python脚本实现文件MD5对比
首先,我们要先从分析报告里获取到已经筛选出的重复文件路径.
我们的分析器报告存储的位置是volume2中,所以我的报告的完整路径应该是:/volume2/strong_report/synoreport/ReaptPhoto/2022-11-02_11-22-21/csv
前面是固定的路径不变,只有2022-11-02_11-22-21 这个是随着每次分析时,按照当前时间命名的文件夹.我们可以利用os.listdir(path)来获取这个文件夹的名字.完成拼接.
拼接完成后,就是解压缩包含csv的zip文件.然后利用字符串分割,把文件路径取出来,进行MD5和文件名长度的对比.最后再进行删除操作.
把python脚本上传到一个目录里
添加计划任务(计划任务的时间要比分析器执行的时间往后去,要先分析文件,再进行删除).
电子邮件通知要先在通知里配置.运行命令 python3 你的python脚本路径
然后运行一下计划任务,看看结果如何. 这个结果是在邮件里哟.
不让放下载地址.咋整?
#!/usr/bin/env python
# coding:utf-8
# Name : remove_picture.py
# Author : junkecm
# Mail : 554680244@qq.com
# Time : 2022/11/1 9:15
# Desc:
import csv
import hashlib
import os
import random
import shutil
import time
import zipfile
def zip_csv_file():
zip_path = '/volume2/strong_report/synoreport/ReaptPhoto/%s/csv/' # 这里需要改成你自己的csv报告的路径
zip_dir = os.listdir('/volume2/strong_report/synoreport/ReaptPhoto/') # 这里就是2022-11-02_11-22-21的上级目录路径
zip_path = zip_path % zip_dir[-1]
zipFile = zipfile.ZipFile(os.path.join(zip_path, 'duplicate_file.csv.zip')) # duplicate_file.csv.zip是固定不变的,不用更改
for file in zipFile.namelist():
zipFile.extract(file, '/volume2/python_shell/') # 这个是你python文件存放的目录,填写你自己的目录
zipFile.close()
# time.sleep(random.randint(3, 9))
def del_file():
files_dict = {}
index = 0
r_index = 0
with open('/volume2/python_shell/duplicate_file.csv', 'r',
encoding='utf-16') as file: # 同上填写你对接python文件执行目录加上你要分析的csv文件名
texts = csv.reader(file)
for item in texts:
index += 1
print("Processing: %s." % str(index))
for i in item:
vid, home, path, size, mtime = i.split('t', 4)
if path == 'File':
pass
else:
path = path.replace('"', '')
md5_hash = hashlib.md5()
try:
with open(path, "rb+") as f:
for byte_block in iter(lambda: f.read(4096), b""):
md5_hash.update(byte_block)
file_md5 = md5_hash.hexdigest()
if files_dict.get(file_md5) is None:
files_dict[file_md5] = path
else:
d_path = files_dict[file_md5]
if len(str(d_path)) < len((path)):
print('Delete File:', path)
os.remove(path)
r_index += 1
except Exception as e:
print('File does not exist or has been deleted')
print('Repeat Files Num:%s.All deleted!' % str(r_index))
if __name__ == '__main__':
zip_csv_file()
del_file()
作者声明本文无利益相关,欢迎值友理性交流,和谐讨论~
















校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案
校验提示文案