python+encode
作者: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或者任务计划里执行
缪雄马3508怎么配置opencv python3.6.1 anaconda -
双妮诗15027221593 ______ 电脑系统:win7 64位,(其他系统类似) 关于Anaconda3-4.4.0下配置OpenCV3.2.01.首先官网下载最新版本的Anaconda3-4.4.0(基于自己的电脑选择32位或64位),该版本已经支持最新的Python3.6;注意:安装过程中:1)安装路径可以改...
缪雄马3508python匹配IP和端口 -
双妮诗15027221593 ______ import reresults = re.findall('(?isu)(\d+)\.(\d+)\.(\d+)\.(\d+)\s*(\d+)', your_html)for...
缪雄马3508python正则表达式函数match和search的区别详解 -
双妮诗15027221593 ______ match()和search()都是python中的正则匹配函数,那这两个函数有何区别呢?match()函数只检测RE是不是在string的开始位置匹配, search()会扫描整个string查找匹配, 也就是说match()只有在0位置匹配成功的话才有返回,如果...
缪雄马3508用python实现计算黑色星期五 -
双妮诗15027221593 ______ #!usr/bin/env python #-*- coding=utf-8 -*- from datetime import * import calendar class BlackFriday(object): def __init__(self,year): self.year=year def display(self): print 'The black Fridays of year %s is(%s-1-1 is %s):' % (self.year,self.year,datetime...
缪雄马3508如何在powershell里运行python和代码 -
双妮诗15027221593 ______ (1)打开powershell.在开始菜单的搜索框输入powershell,回车即可打开. (2)运行python.在打开的powershell里直接输入python,回车即可打开.如果python不能被识别,需要先在powershell下输入并执行以下指令: [Environment]::...
缪雄马3508如何运行python脚本 -
双妮诗15027221593 ______ Python安装好以后,在开始菜单会看到一个idle工具(一个增强的交互命令行解释器窗口)以及一个自带的编辑器.在任意目录新建一个.py文件或者保存的时候以.py结尾,用记事本、Python自带的编辑器或者其他编辑器如Sublime Text或者NotePad++都行.如果你使用的是idle,直接按F5就能在Python自带的命令行查看结果 如果是用记事本或者其他编辑器,快捷键win+R调出运行目录,输入cmd,然后在命令行里进入这个目录,输入python [filename].py或者[filename].py就可以看到运行结果了
缪雄马3508谁能写一个正确的python “hello world” 完整的! -
双妮诗15027221593 ______ #!python # encoding: utf-8 def hello(name='world'): return 'Hello %(name)s' % dict(name=name) if __name__ == '__main__': print(hello()) ###### or just: ############# print('hello world') # 关键看你对完整的定义是什么
缪雄马3508python 16进制转字符串 -
双妮诗15027221593 ______ #将16进制字符转成二进制字符def str2byte(s): base='0123456789ABCDEF' i=0 s = s.upper() s1='' while i < len(s): c1=s[i] c2=s[i+1] ...
缪雄马3508用Python 怎样实现一个九九乘法表 -
双妮诗15027221593 ______ table_9x9.py #! /usr/bin/python # Filename : table_9x9.py # Author : Jesse # Date : 2011/08/13 21:50 print '\n9x9 Table\n' for i in range(1, 10) : for j in range(1, i+1) : print j, 'x', i, '=', j*i, '\t', # print '%d x %d = %d\t' %(j, i, j*i), print '\n' print '\...
缪雄马3508谁有PYTHON编写的凯撒密码的加密和解密代码? -
双妮诗15027221593 ______ 给你写了一个. def convert(c, key, start = 'a', n = 26): a = ord(start) offset = ((ord(c) - a + key)%n) return chr(a + offset) def caesarEncode(s, key): o = "" for c in s: if c.islower(): o+= convert(c, key, 'a') elif c.isupper(): o+= convert(c, key, 'A') else: o+= ...