Python脚本小工具:清除电脑微信老旧文件

电脑微信使用时间长了,占用空间会越来越大,主要来自聊天中的视频、图片、文件以及缓存、表情图片等。磁盘空间不足时想清理,但又不想全部删除。这里分享一个Python脚本,按历史时间清除老旧文件。

首先,假设您想清除6个月以前的文件,即保留最近6个月的文件,可以根据自身情况调整。定义一个全局变量:

g_MAX_MONTHS = 6  # keep latest 6 months

然后,找到微信目录下占用空间的几个文件夹。下面代码中的XXXXX为微信账号,需要把整个文件路径挨个替换成实际的路径。

# 聊天记录中的文件
paths = [\
r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\Cache',\
r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\File',\
r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\Image',\
r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\Sns\Cache',\
r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\Video']
# 聊天中缓存的表情文件
p = r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\CustomEmotion'

以下是完整代码:

'''
Scripting to clear WeChat history record by batch

idea:
for each dir in path_list
    for each folder in dir
        if now - name > 6 months
            delete folder
'''

import time
import os
import shutil

g_MAX_MONTHS = 6  # keep latest 6 months

def istarget(f):  # check if the folder is too old 
    pattern = '2021-12'
    
    if len(f) != len(pattern):
        return False
    
    if f[4] != '-':
        return False
        
    if not f[:4].isdigit():
        return False
        
    if not f[5:].isdigit():
        return False

    y = int(f[:4])
    m = int(f[5:])
    now = time.localtime()
    global g_MAX_MONTHS
    if (now.tm_year * 12 + now.tm_mon) - (y * 12 + m) <= g_MAX_MONTHS:
        return False
        
    return True

def deldir(path):  # delete dir and subfiles
    try:
        shutil.rmtree(path)
    except Exception as e:
        print(e)
        cmd = 'rmdir /s /q "' + path + '"'
        os.system(cmd)

paths = [\
r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\Cache',\
r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\File',\
r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\Image',\
r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\Sns\Cache',\
r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\Video']

for p in paths:
    for i in os.listdir(p):
        f = os.path.join(p, i)
        if os.path.isdir(f) and istarget(i):
            print(f)
            #shutil.rmtree(f)  # PermissionError: [WinError 5] access refused
            deldir(f)

p = r'D:\download\Wechat\Personal\WeChat Files\XXXXX\FileStorage\CustomEmotion'
for i in os.listdir(p):
    f = os.path.join(p, i)
    mtime = os.path.getmtime(f)
    now = time.time()
    if now - mtime > g_MAX_MONTHS / 2 * 30 * 24 * 3600:  # keep only 3 months
        print(f)
        if os.path.isdir(f):
            #shutil.rmtree(f)  # sometimes error happens: PermissionError: [WinError 5] access refused
            deldir(f)
        else:
            os.remove(f)

print("Task completed!")

其中,表情文件设置为只保留3个月,可以根据情况自行调整。
运行过程中发现用shutil.rmtree()删除文件夹有时会报权限错误,所以自己写了一个deldir()的函数。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值