删除电脑微信历史文件
```python#!/usr/bin/env python
# encoding: utf-8
'''
@desc:
Windows清除pc微信几个月前的历史文件
只会删除FileStorage下的数据
'''
import os
import re
import shutil
import datetime
from dateutil.relativedelta import relativedelta
def get_wechat_file_path():
"""
获取微信文件路径
:return: ['C:\\Users\\xxx\\Documents\\WeChat Files']
"""
print('开始匹配您的微信路径')
root_path = r'C:\Users'
wechat_paths = []
for root, dirs, files in os.walk(root_path):
if re.search(r'Documents\\WeChat Files$', root):
wechat_paths.append(root)
if wechat_paths:
print('微信路径匹配完成')
else:
print('微信路径匹配失败,退出')
return wechat_paths
if __name__ == '__main__':
delta_months = int(input('保留最近几个月的数据:'))
now_date = datetime.datetime.utcnow() + datetime.timedelta(hours=8)
rel_month = relativedelta(months=delta_months)
standard_day = (now_date - rel_month).strftime("%Y-%m")
print('准备删除{}及其以前的数据'.format(standard_day))
wechat_paths = get_wechat_file_path()
for wechat_path in wechat_paths:
print('正在处理{}'.format(wechat_path))
users = ]# 剔除小程序及头像
for user in users:
path = os.path.join(wechat_path, user, 'FileStorage')
for root, dirs, files in os.walk(path):
file_month = re.findall('\d{4}-\d{2}$', root)
if file_month and file_month <= standard_day:
# 删除delta_months个月之前的数据
print('正在删除目录:{}'.format(root))
shutil.rmtree(root)
print('处理完成')
```
页:
[1]