python中33个保留字中文
作者:大橡皮啊
最终效果:
删除重复文件,并保留创建日期更为久远的文件
上篇文章写了,利用群晖的存储空间分析器结合python来查找重复文件.经人提醒,发现完全不需要用到存储分析器.因为那个文章编辑次数到限制了.无法编辑了.只能重新开贴. 有一点小小的设置.看下图
file_type是一个文件类型的列表.在里面添加你想要删除的文件类型即可.每个文件类型之间用逗号隔开.
import os
import hashlib
# 只删除以下列表中的重复文件类型.如果想删除其他类型的文件,自己添加一下就行了
file_type = ['.jpg', '.jpeg', '.png', '.gif', '.psd', '.bmp', '.webp', '.mp4', '.mkv', '.avi', '.mov', 'mpeg', 'mpg',
'.rar', '.zip']
check_files = []
def remove_reapt_files():
for root, dirs, files in os.walk(r'/volume1/homes/xxxxxx/Drive/Moments/'):
for name in files:
# print(name)
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 = {}
r_index = 0
print('Fiels Num:%s' % len(check_files))
for value in check_files:
md5_hash = hashlib.md5()
try:
with open(value, "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' % value)
if files_dict.get(file_md5) is None:
files_dict[file_md5] = value
else:
d_path = files_dict[file_md5]
d_path_stats = os.stat(d_path)
file_stats = os.stat(value)
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] = value
print('Delete File:', d_path)
r_index += 1
else:
os.remove(value)
print('Delete File:', value)
r_index += 1
except Exception as e:
pass
#print('File does not exist or has been deleted')
print('File Count:%s, Repeat Files Num:%s. All deleted!' %( len(check_files),str(r_index)))
if __name__ == '__main__':
remove_reapt_files()
文件下载之后,要添加到计划任务中.
先打开群晖上的File Station 新建一个文件夹.我这边取名叫 python_shell.然后把脚本文件复制到文件夹下.
然后打开计划任务,添加一个计划任务.执行频率.看个人喜好.运行命令就写python3 加上脚本的路径
这样就大功告成了.看看效果吧.
@eadir文件夹是群晖全局索引文件.所以删除的文件要比实际的重复要多.因为索引也删除了.
武坚咬1368python运行显示空值,用的是3.3版本的,怎么回事呢 -
潘到备17664142594 ______ 查看下环境变量PATH中是否我多个D:\Python33这样的变量,如果有多个,删除多余的,仅保留一个
武坚咬1368python一些数在列表中,如:L = [1, 3, 2, 1, 6, 4, 2, ....., 98, 82] -
潘到备17664142594 ______ 第一个要求其实很简单,可以讲列表转为集合,python中列表是用于储存不限数量可重复可修改元素的,集合是用于储存不限数量不可重复可修改元素的(也有不可修改集合),只要将列表转为集合再转回列表,就可以满足第一个要求;第二个...
武坚咬1368如何在python中统计数字在文本中出现的次数 -
潘到备17664142594 ______ 一楼稍改: import re text="""123 we are -3.13, and 342 or 58.48 """ reg=re.compile(r"((-)?\d+(\.\d+)?)") finded = map(lambda n:n[0], reg.findall(text)) print finded, len(finded) 执行结果: >>> ['123', '-3.13', '342', '58.48'] 4
武坚咬1368python中的set如何只取结果 -
潘到备17664142594 ______ >>> b=list(s)>>> print b [1, 2, 3, 4]