友快網

導航選單

利用python刪除群暉中的重複檔案

作者:大橡皮啊

最終效果:

刪除重複檔案,並保留建立日期更為久遠的檔案

上篇文章寫了,利用群暉的儲存空間分析器結合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資料夾是群暉全域性索引檔案。所以刪除的檔案要比實際的重複要多。因為索引也刪除了。

開啟App看更多精彩內容

上一篇:梁靖崑被禁賽,馬龍因類似動作引發討論,龍隊最新表態霸氣十足
下一篇:上海環城生態公園帶規劃建設,確定四個區域11項重點工作