吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6461|回复: 10
收起左侧

[Python 转载] 分享一个用python写的window清理缓存垃圾小程序

[复制链接]
Pers丶零 发表于 2017-11-20 15:33
本帖最后由 Pers丶零 于 2017-11-20 15:44 编辑

贴上源代码,仅供参考。
[Python] 纯文本查看 复制代码
# coding: utf-8
import os
import shutil

del_extension = ['.tmp', '._mp', '.log', '.gid', '.chk', '.old', '.xlk', '.bak']
del_userprofile = ['cookies', 'recent', 'Temporary Internet Files', 'Temp']
del_windir = ['prefetch', 'temp']
# 获取系统盘
root_sys_drive = os.environ['systemdrive'] + '\\'
# 获取用户目录
root_user_profile = os.environ['userprofile']
# 获取 Windows 目录
root_win_dir = os.environ['windir']


def del_dir_or_file(root):
    try:
        if os.path.isfile(root):
            # 删除文件
            os.remove(root)
            print ('file: ' + root + ' removed')
        elif os.path.isdir(root):
            # 删除文件夹
            shutil.rmtree(root)
            print ('directory: ' + root + ' removed')
    except WindowsError:
        print ('failure: ' + root + " can't remove")

for roots, dirs, files in os.walk(root_sys_drive, topdown=False):
    # 生成并展开以 root 为根目录的目录树,参数 topdown 设定展开方式从底层到顶层
    for every_file in files:
        # 获取文件扩展名
        file_extension = os.path.splitext(every_file)[1]
        if file_extension in del_extension:
            # 组合文件完整路径
            full_path = os.path.join(roots, every_file)
            # 调用函数删除文件
            del_dir_or_file(full_path)
for every_dir in del_userprofile:
    # 组合文件夹完整路径
    full_path = os.path.join(root_user_profile, every_dir)
    # 调用函数删除文件夹
    del_dir_or_file(full_path)
for every_dir in del_windir:
    full_path = os.path.join(root_win_dir, every_dir)
    del_dir_or_file(full_path)
print ('\nfinished')

求给评分热心!!!
6666.png

免费评分

参与人数 7吾爱币 +7 热心值 +7 收起 理由
a1239761234 + 1 + 1 谢谢@Thanks!
sec0ces + 1 + 1 支持一下!
zhuce000201 + 1 + 1 谢谢@Thanks!
chucklee + 1 + 1 用心讨论,共获提升!
EZ195547718 + 1 + 1 感谢您的宝贵建议,我们会努力争取做得更好!
b19341 + 1 + 1 热心回复!
wkfy + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

aikele 发表于 2017-11-20 15:40
支持 学习中
zongrong12345 发表于 2017-11-20 16:27
huichen2012 发表于 2017-11-20 16:30
shubiao05 发表于 2017-11-20 16:33
Windows10平台下,用Python2.7运行,非常完美,不错的程序,赞一个!

楼主辛苦!
ftmovie 发表于 2017-11-20 19:37 来自手机
感谢楼主,谢谢分享
joyc 发表于 2017-11-20 22:54
楼主写的是py3,截图是删除出错啊。
hbkccccc 发表于 2017-11-20 23:50
支持 学习中
chucklee 发表于 2017-11-21 09:03
好 很好 非常好
zhuce000201 发表于 2017-11-22 09:34 来自手机
学习学习,谢谢楼主分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-4 15:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表