磁盘清理工具(python)

文章目录

  • 磁盘清理工具(python)
  • 一、python代码
  • 二、打包exe

一、python代码

# 删除文件夹下面的所有文件(只删除文件,不删除文件夹)
import os
import shutil
import stat
import threading
import time
import tkinter as tk
from tkinter import ttk, END
from time import sleep
from tkinter import messagebox
from tkinter.filedialog import askdirectory# Temp-临时文件
path_data1 = "C:\\Windows\\Temp"
# Prefetch-访问记录
path_data2 = "C:\\Windows\\Prefetch"
# Download-系统更新时下载的补丁和一些安装包等
path_data3 = "C:\\Windows\\SoftwareDistribution\\Download"
# LogFiles-系统日志及操作记录
path_data4 = "C:\\Windows\\System32\\LogFiles"
path_data_diy = ""# python删除文件的方法 os.remove(path)path指的是文件的绝对路径,如:
# os.remove(r"E:\code\practice\data\1.py")#删除文件
# os.rmdir(r"E:\code\practice\data\2")#删除文件夹(只能删除空文件夹)
# shutil.rmtree(r"E:\code\practice\data\2")#删除文件夹
# path_data = "E:\code\practice\data"#def thread_it(func, *args):"""将函数打包进线程"""# 创建t = threading.Thread(target=func, args=args)# 守护 !!!t.setDaemon(True)# 启动t.start()# 阻塞--卡死界面!# t.join()class GUI:def __init__(self):self.root = tk.Tk()self.root.title("DDPl文件操作")self.root.configure(bg='#2c3038')self.root.option_add('*Font', '楷体')# self.root.geometry("500x200+1100+150")# 程序运行时在屏幕中间打开sw = self.root.winfo_screenwidth()sh = self.root.winfo_screenheight()ww = 1055wh = 550x = (sw - ww) / 2y = (sh - wh) / 2self.root.geometry("%dx%d+%d+%d" % (ww, wh, x, y))self.root.resizable(False, False)self.root.update()self.root.wm_attributes('-topmost', 1)self.interface()def interface(self):self.CheckVar1 = tk.IntVar()self.CheckVar2 = tk.IntVar()self.CheckVar3 = tk.IntVar()self.CheckVar4 = tk.IntVar()self.C1 = tk.Checkbutton(self.root, text="Temp-临时文件", variable=self.CheckVar1, onvalue=1, offvalue=0,fg='#d9f5ff', bg='#2c3038', selectcolor='#2c3344', activebackground='#2c3038',activeforeground='#60b6fc')self.C2 = tk.Checkbutton(self.root, text="Prefetch-访问记录", variable=self.CheckVar2, onvalue=1, offvalue=0,fg='#d9f5ff', bg='#2c3038', selectcolor='#2c3344', activebackground='#2c3038',activeforeground='#60b6fc')self.C3 = tk.Checkbutton(self.root, text="Download-系统更新补丁", variable=self.CheckVar3, onvalue=1, offvalue=0,fg='#d9f5ff', bg='#2c3038', selectcolor='#2c3344', activebackground='#2c3038',activeforeground='#60b6fc')self.C4 = tk.Checkbutton(self.root, text="LogFiles-系统日志", variable=self.CheckVar4, onvalue=1, offvalue=0,fg='#d9f5ff', bg='#2c3038', selectcolor='#2c3344', activebackground='#2c3038',activeforeground='#60b6fc')self.C1.select()self.C2.select()self.C3.select()self.C4.select()self.C1.grid(row=0, column=0, ipadx=10, ipady=10, padx=3, pady=10)self.C2.grid(row=0, column=1, ipadx=10, ipady=10, padx=3, pady=10)self.C3.grid(row=0, column=2, ipadx=10, ipady=10, padx=3, pady=10)self.C4.grid(row=0, column=3, ipadx=10, ipady=10, padx=3, pady=10)self.Button0 = tk.Button(self.root, text="清理基本C盘垃圾", command=lambda: thread_it(self.event_清理基本C盘垃圾), width=10,bg='#4a8e53', fg='#d9f5ff', activebackground='#4d535f', activeforeground='#fdfdfd')self.Button0.grid(row=0, column=4, ipadx=20, ipady=10, padx=5, pady=10)self.w1 = tk.Entry(self.root, textvariable='请输入目标路径', bg='#25272c', fg='#b2b2b2')self.w1.grid(row=1, column=0, columnspan=4, ipadx=290, ipady=8, padx=5, pady=10)self.Button1 = tk.Button(self.root, text="选择目标文件夹", command=lambda: thread_it(self.event_选择目标文件夹), width=10,bg='#4780ac', fg='#d9f5ff', activebackground='#4d535f', activeforeground='#fdfdfd')self.Button1.grid(row=1, column=4, ipadx=20, ipady=10, padx=5, pady=10)self.Button2 = tk.Button(self.root, text="清理目标文件夹", command=lambda: thread_it(self.event_清理目标文件夹), width=10,bg='#4a8e53', fg='#d9f5ff', activebackground='#4d535f', activeforeground='#fdfdfd')self.Button2.grid(row=1, column=5, ipadx=20, ipady=10, padx=5, pady=10)self.Button3 = tk.Button(self.root, text="清空输出信息", command=lambda: thread_it(self.event_清空输出信息), width=10,bg='#2c3038', fg='#d9f5ff', activebackground='#4d535f', activeforeground='#fdfdfd')self.Button3.grid(row=0, column=5, ipadx=20, ipady=10, padx=5, pady=10)self.text = tk.Text(self.root, bg='#25272c', fg='#777c8a')self.text.grid(row=2, column=0, columnspan=6, ipadx=195, padx=10, pady=10)# 新建滚动条self.scroll = tk.Scrollbar()# 两个控件关联self.scroll.config(command=self.text.yview)self.text.config(yscrollcommand=self.scroll.set)def event_清理基本C盘垃圾(self):try:if self.CheckVar1.get() == 1:self.routineCleanup1()if self.CheckVar2.get() == 1:self.routineCleanup2()if self.CheckVar3.get() == 1:self.routineCleanup3()if self.CheckVar4.get() == 1:self.routineCleanup4()except Exception as e:print(e)def event_选择目标文件夹(self):path_ = askdirectory()  # 使用askdirectory()方法返回文件夹的路径if path_ == "":self.w1.get()  # 当打开文件路径选择框后点击"取消" 输入框会清空路径,所以使用get()方法再获取一次路径else:path_ = path_.replace("/", "\\")  # 实际在代码中执行的路径为“\“ 所以替换一下print(path_)self.w1.delete(0, END)self.w1.insert(0, path_)def event_清理目标文件夹(self):try:data_path = self.w1.get()if data_path is not None:time1 = time.time()current_time = time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(time1))self.text.insert(tk.INSERT, "正在清理目标文件" + data_path + current_time + '\n')self.text.see(END)self.del_file(data_path)time2 = time.time()self.text.insert(tk.INSERT, "清理用时" + str(round((time2 - time1), 2)) + 's' + '\n')self.text.see(END)except Exception as e:self.text.insert(tk.INSERT, "   " + str(e) + '\n')self.text.see(END)print(e)def event_清空输出信息(self):try:self.text.delete('1.0', END)except Exception as e:print(e)passdef del_file(self, path_data):if len(os.listdir(path_data)) == 0:self.text.insert(tk.INSERT, "  无垃圾可清理" + '\n')self.text.see(END)print("  无垃圾可清理")returnn = 0for i in os.listdir(path_data):  # os.listdir(path_data)#返回一个列表,里面是当前目录下面的所有东西的相对路径# file_data = path_data + "\\" + i  # 当前文件夹的下面的所有东西的绝对路径path = os.path.join(path_data, i)if os.path.isdir(path):try:# 使用shutil模块shutil.rmtree(path)except Exception as error:os.system('rd /s /q %s' % path)self.text.insert(tk.INSERT, '  已清除文件夹 ' + path + '\n')self.text.see(END)print('  已清除文件夹 ' + path)elif os.path.isfile(path):try:# 使用os模块删除os.remove(path)except Exception as error:# 使用windows命令行强制删除# os.system('del' + path + '/S')os.system("del /f /q %s" % path)self.text.insert(tk.INSERT, '  已清除文件 ' + path + '\n')self.text.see(END)print('  已清除文件 ' + path)n += 1if n > 0:self.text.insert(tk.INSERT, '此路径共清除文件数: ' + str(n) + '\n')self.text.see(END)def routineCleanup1(self):try:time1 = time.time()current_time = time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(time1))self.text.insert(tk.INSERT, "正在清理Temp-临时文件的垃圾……" + current_time + '\n')self.text.see(END)print("正在清理Temp-临时文件的垃圾……" + current_time)# 利用以下语言获得文件夹的写入权限os.chmod(path_data1, stat.S_IRWXU)self.del_file(path_data1)time2 = time.time()self.text.insert(tk.INSERT, "  清理用时" + str(round((time2 - time1), 2)) + 's' + '\n')self.text.see(END)except Exception as e:self.text.insert(tk.INSERT, "   " + str(e) + '\n')self.text.see(END)print("   " + str(e))def routineCleanup2(self):try:time1 = time.time()current_time = time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(time1))self.text.insert(tk.INSERT, "正在清理Prefetch-访问记录的垃圾……" + current_time + '\n')self.text.see(END)print("正在清理Prefetch-访问记录的垃圾……" + current_time)os.chmod(path_data2, stat.S_IRWXU)self.del_file(path_data2)time2 = time.time()self.text.insert(tk.INSERT, "  清理用时" + str(round((time2 - time1), 2)) + 's' + '\n')self.text.see(END)except Exception as e:self.text.insert(tk.INSERT, "   " + str(e) + '\n')self.text.see(END)print("   " + str(e))def routineCleanup3(self):try:time1 = time.time()current_time = time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(time1))self.text.insert(tk.INSERT, "正在清理Download-系统补丁的垃圾……" + current_time + '\n')self.text.see(END)print("正在清理Download-系统补丁的垃圾……" + current_time)# 利用以下语言获得文件夹的写入权限os.chmod(path_data3, stat.S_IRWXU)self.del_file(path_data3)time2 = time.time()self.text.insert(tk.INSERT, "  清理用时" + str(round((time2 - time1), 2)) + 's' + '\n')self.text.see(END)except Exception as e:self.text.insert(tk.INSERT, "   " + str(e) + '\n')self.text.see(END)print("   " + str(e))def routineCleanup4(self):try:time1 = time.time()current_time = time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(time1))self.text.insert(tk.INSERT, "正在清理LogFiles-系统日志的垃圾……" + current_time + '\n')self.text.see(END)print("正在清理LogFiles-系统日志的垃圾……" + current_time)# 利用以下语言获得文件夹的写入权限os.chmod(path_data4, stat.S_IRWXU)self.del_file(path_data4)time2 = time.time()self.text.insert(tk.INSERT, "  清理用时" + str(round((time2 - time1), 2)) + 's' + '\n')self.text.see(END)except Exception as e:self.text.insert(tk.INSERT, "   " + str(e) + '\n')self.text.see(END)print("   " + str(e))if __name__ == "__main__":a = GUI()a.root.mainloop()

注意:因为c盘权限问题,直接运行py文件会报错。

二、打包exe

在命令行窗口中先进入py文件的目录,之后输入pyinstaller XXXX.py
在生成的dist文件夹中找到exe文件,以管理员身份运行文件。

链打包好的exe文件: 磁盘清理工具.

磁盘清理工具(python)相关推荐

  1. 如何在Windows Server 2008 R2没有磁盘清理工具的情况下使用系统提供的磁盘清理工具...

    2019独角兽企业重金招聘Python工程师标准>>> 今天,刚好碰到服务器C盘空间满的情况,首先处理了临时文件和有关的日志文件后空间还是不够用,我知道清理C盘的方法有很多,但今天只 ...

  2. VC.NET扩展Windows磁盘清理工具的功能

    介绍了Windows磁盘清理工具二次开发的扩展接口,对其COM接口加以分解,并运用ATL库具体实现了清理"*.tmp"临时文件的功能. 关键词 磁盘清理工具.ATL库.COM接口. ...

  3. Windows Server 2008 没有磁盘清理工具的情况下使用系统提供的磁盘清理工具

    服务器C盘空间满的情况,首先处理了临时文件和有关的日志文件后空间还是不够用,清理C盘的方法有很多,分享一下如何在Windows Server 2008 没有磁盘清理工具的情况下使用系统提供的磁盘清理工 ...

  4. win10系统自带清除磁盘垃圾工具———磁盘清理工具

    由于win10系统有自带的杀毒软件,可以不用额外下载诸如金山毒霸.360等杀毒软件.那么,当我们想清理一些系统垃圾,释放存储空间时,应该怎么办呢?这里推荐使用win10自带的磁盘清理工具,步骤如下: ...

  5. CMD快捷指令之打开磁盘清理工具

    CMD快捷指令在Windows用户时提高了很大的效率,只是其中的一个用法之一. 1.按按键win+R(个人用的是英文系统哟(^U^)ノ~YO) 2.在输入框中输入cmd. 然后确定. 3. 在C:\U ...

  6. Disk Expert 3.6.1 可视化磁盘清理工具

    Disk Expert 是一款可视化Mac磁盘文件清理工具.Disk Expert 以可视化的图表形式显示您的磁盘空间使用情况,通过点击图表上的扇区可以快速查看对应目录下的文件列表,默认按文件大小排序 ...

  7. Win11 没有磁盘清理工具,如何清理磁盘旧的windows.old文件

    win11 磁盘右键没有了"磁盘清理工具" 如果想清理文件,可以打开设置(Win+I)系统-存储-清理建议 您也可以打开"存储感知",系统会在需要时帮你自动清理 ...

  8. 服务器系统盘清理工具,Windows添加磁盘清理工具的方法

    Windows Server 2008 R2中并没有预装自带的磁盘清理工具,需要手动配置一下才能使用磁盘清理工具,很多时候系统更新之后会产生很多垃圾文件,使用第三方软件清理又不放心,因此这里分享一下如 ...

  9. windows server 2008 r2如何找到自带的磁盘清理工具

    服务器不知道什么原因,一直在变大,直到没有空间,准备通过系统自带的磁盘清理工具清理一下的时候发现找不到,在网上找到了这个文章,亲测有效,需要的可以试试 该文章以下内容转载自:如何在Windows Se ...

最新文章

  1. 数据库分库分表(sharding)系列
  2. MarkDown入门及技巧
  3. Python 爬虫利器三之 Xpath 语法与 lxml 库的用法
  4. ML《决策树(三)CART》
  5. eclipseEE连接mysql_mysql-eclipseEE连接MySQL出现问题,哪位大神可以帮我看看*^__^*,非常感谢...
  6. SSZipArchive的使用详解和遇到的问题
  7. 这些严重的 Slack桌面劫持漏洞仅值区区1750美元?
  8. 跳转html时请求头怎么取,如何获取a链接的请求头信息?
  9. [Unity]技巧分享:更改Unity Asset Store 默认下载资源位置的方法
  10. 真 Navicat Premium 12.0.27 for Mac 破解版
  11. 谈一谈软件系统的可用性
  12. 看清我辈基本盘!罗振宇2020跨年演讲全文来了(未删减)
  13. 如何接入易班第三方登录
  14. 云呐|常用的固定资产盘点方法有哪些
  15. 电路中滤波电容和退耦电容_电容在电路中究竟有多少种应用?
  16. 不需要解压使用对pdf文件进行压缩
  17. Box2D物理引擎入门
  18. 商人过河c语言实验报告,商人过河C语言程序编程
  19. EFR32--如何在EFR32程序中修改UUID
  20. MySQL之增删改以及外键作用

热门文章

  1. 实例六:MATLAB APP design- 凸透镜成像光学研究小工具
  2. python转成exe运行出错_使用py2exe将.py转换为.exe:无法运行程序
  3. 关于小米手机“与已安装应用签名不同”的问题
  4. python反编译luac_LuaJIT反编译总结
  5. 零售业无线技术应用--IP导航
  6. SDCC 2016系列多站回顾:精彩集锦 百家争鸣(附PPT下载)
  7. ~scanf()和scanf()!=EOF
  8. Matlab App Designer 【03】绘制基本测试函数图像以及摸鱼听歌
  9. (附源码)python音乐电台推荐系统的设计与实现 毕业设计 301210
  10. 在VNC下远程安装Linux系统