用Python整理微信文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import hashlib

nowpath=os.path.abspath('.')
file_in_every_months=os.listdir(nowpath)
file_in_every_months.remove('wechat_file_cleaner.py')
try:
file_in_every_months.remove('useful_files')
except:
os.makedirs('useful_files')

now_files=os.listdir(nowpath+'\\useful_files\\')
md5dir={}
for now_file in now_files:
with open(nowpath+'\\useful_files\\'+now_file,'rb') as temp:
m=hashlib.md5(temp.read()).hexdigest()
md5dir[m]=['useful_files\\'+now_file]


for month in file_in_every_months:
monthpath=nowpath+'\\'+month
files=os.listdir(monthpath)
for ordinary_file in files:
filepath=monthpath+'\\'+ordinary_file
if ordinary_file[-4:]=='.gif':
cmd='del "%s"\\"%s"'%(month,ordinary_file)
else:
with open(filepath,'rb') as f:
m=hashlib.md5(f.read()).hexdigest()
if m in md5dir:
md5dir[m].append(month+'\\'+ordinary_file)
cmd='del "%s"\\"%s"'%(month,ordinary_file)
else:
md5dir[m]=[month+'\\'+ordinary_file]
cmd='move "%s"\\"%s" useful_files\\"%s"'%(month,ordinary_file,ordinary_file)

print(cmd)
try:
os.system(cmd)
except:
pass

for key in md5dir:
if len(md5dir[key])==1:
pass
else:
print('合并'+str(md5dir[key]))
-------------end-------------