添加到计划任务每月运行
from datetime import datetime
import os
# 这里使用了Windows的环境变量,需在计划任务中指定你的用户
PATH_SCREENSHOTS = r'C:\Users\%USERNAME%\Pictures\Screenshots'
PATH_7Z = r'C:\Program Files\7-Zip\7z.exe' if os.path.isfile(r'C:\Program Files\7-Zip\7z.exe') else r'7z'
if os.system(PATH_7Z) != 0:
print('找不到7z安装,尝试重新安装到默认文件夹或添加到环境变量')
exit(1)
img_list = list(filter(lambda x: str(x).endswith(('.jpg', '.png')), os.listdir(PATH_SCREENSHOTS)))
# 如果没有截图就退出
if not img_list:
exit(0)
date_ym = datetime.today().strftime("%Y%m")
cmd = f'cd {PATH_SCREENSHOTS} && {PATH_7Z} a {date_ym}.7z {''.join(map(lambda x: f' "{x}"', img_list))}'
# 压缩后删除原文件
os.system(cmd)
for image in img_list:
os.remove(f'{PATH_SCREENSHOTS}\\{image}')
标签:img,Windows,SCREENSHOTS,list,Python,浅写,PATH,os,7z
From: https://www.cnblogs.com/lzakou/p/17874340.html