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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2175|回复: 27
收起左侧

[原创工具] 【原创开源】清理指定路径下所有0字节文件

  [复制链接]
2468567855 发表于 2023-5-29 00:18
在清理电脑视频图片文件时发现之前硬盘满了之后所遗留下来的0字节文件,自己又懒得一个个去找就直接用python写了个工具用来清理这些0字节文件


功能:

1、自定义目录路径清理该路径下所有子目录的全部0字节文件

2、检索时间比较长做了个进度条展示进度、百分比、剩余时间


下载链接:https://wwi.lanzoup.com/ihct70xk8e5g



代码块:

[Python] 纯文本查看 复制代码
from tkinter import messagebox

import os

from tkinter import filedialog, messagebox

import tkinter as tk

from tkinter import ttk

from datetime import timedelta

import time


def delete_zero_byte_files(directory):

    count = 0

    total_count = 0

    for root, dirs, files in os.walk(directory):

        total_count += len(files)

    progress_bar['maximum'] = total_count

    start_time = time.monotonic()

    for root, dirs, files in os.walk(directory):

        for file in files:

            file_path = os.path.join(root, file)

            if os.path.isfile(file_path) and os.path.getsize(file_path) == 0:

                os.remove(file_path)

                count += 1

            progress_bar['value'] += 1

            progress_percent.set(f'已完成{progress_bar["value"] / progress_bar["maximum"]:.2%}')

            elapsed_time = time.monotonic() - start_time

            estimated_time_left = (total_count - progress_bar['value']) / progress_bar['value'] * elapsed_time

            estimated_time_left_str = str(timedelta(seconds=estimated_time_left))

            progress_time_left.set(f'预计还需要{estimated_time_left_str}才能完成')

            progress_window.update() # 更新窗口

    end_time = time.monotonic()

    elapsed_time_str = str(timedelta(seconds=end_time - start_time))

    messagebox.showinfo('提示', f'已删除{count}个0字节文件!\n用时{elapsed_time_str}')

    progress_window.destroy() # 关闭进度条窗口


def select_directory():

    directory = filedialog.askdirectory(title='请选择待删除的目录路径')

    if directory: # 用户选择了目录

        global progress_window, progress_bar, progress_percent, progress_time_left

        progress_window = tk.Toplevel(root)

        progress_window.title('删除0字节文件')

        progress_percent = tk.StringVar()

        progress_percent.set('')

        progress_time_left = tk.StringVar()

        progress_time_left.set('')

        progress_percent_text = tk.Label(progress_window, textvariable=progress_percent)

        progress_percent_text.pack(padx=10, pady=5)

        progress_time_left_text = tk.Label(progress_window, textvariable=progress_time_left)

        progress_time_left_text.pack(padx=10, pady=5)

        progress_bar = ttk.Progressbar(progress_window, orient='horizontal', mode='determinate')

        progress_bar.pack(fill='x', padx=10, pady=10)

        progress_window.geometry('400x150')

        progress_window.resizable(False, False)

        delete_zero_byte_files(directory)


if __name__ == '__main__':

    root = tk.Tk()

    root.geometry('300x100')

    root.resizable(False, False)

    select_directory_button = tk.Button(root, text='选择需要删除0字节文件的目录路径', command=select_directory)

    select_directory_button.pack(padx=10, pady=10)

    root.mainloop() # 进入主循环

主界面.png
完成.png
目录选择.png
进度.png

免费评分

参与人数 8吾爱币 +13 热心值 +8 收起 理由
cbkin888 + 1 + 1 大佬,想问问这个能不能清理 空文件夹
Zzzhou-Sco + 1 谢谢@Thanks!
缺牙巴 + 2 + 1 用心讨论,共获提升!
a85401234 + 1 + 1 谢谢@Thanks!
抱薪风雪雾 + 1 + 1 谢谢@Thanks!
shandian281 + 1 + 1 用心讨论,共获提升!
yanglinman + 1 热心回复!
Hmily + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

本帖被以下淘专辑推荐:

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

 楼主| 2468567855 发表于 2023-6-3 15:55
cbkin888 发表于 2023-6-3 13:37
大佬,想问问这个能不能清理空文件夹

不可以,这个只能清理文件,文件夹不支持
lizhipei78 发表于 2023-5-30 19:02
Evthing 右键搜索需要 处理的文件夹,排序,把0字节的全部删除
lcg888 发表于 2023-5-29 12:06
不错不错  吾爱有你更精彩 感谢这么精彩的原创内容
xiaomoge123 发表于 2023-5-29 12:07
和论坛里https://www.52pojie.cn/thread-1711507-1-1.html 类似的软件!
sw1912 发表于 2023-5-29 16:40
先下为敬
orange8030 发表于 2023-5-29 19:24
可以可以,很便捷的工具了,试试看,应该不会误删吧
zj23308 发表于 2023-5-30 02:28
感谢分享,正为0文件夹发愁呢
shandian281 发表于 2023-5-30 07:48
很实用的小工具,感谢分享。
a85401234 发表于 2023-5-30 13:04
感谢分享谢谢!
抱薪风雪雾 发表于 2023-5-30 13:12
有点不太敢用啊
joylj6 发表于 2023-5-30 13:59
对于用了几年的电脑来说很实际。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 提醒:禁止复制他人回复等『恶意灌水』行为,违者重罚!

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

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

GMT+8, 2024-4-20 04:00

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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