使用Python查找大文件的实用脚本

C盘满了,写了一个python脚本,2分多钟能找到比较大的文件,然后手动删除或者迁移D盘,最后发现是微信小程序开发工具缓存文件太多了,腾出来10个G念头通达了,这里备份一下脚本。

运行工具:PyCharm 2024.1.3 (Community Edition)

【完整代码】

python 复制代码
import os
import threading
import time
import sys
from threading import Event

def is_large(file_path, threshold_mb):
    """判断文件大小是否超过指定MB阈值"""
    return os.path.getsize(file_path) / (1024 * 1024) > threshold_mb

def show_loading_animation(stop_event, interval=0.5):
    """显示简易的文本加载动画,直到接收到停止信号"""
    loading_chars = ['.', '..', '...', '....']
    total_cycles = int(interval * 10)
    cycle_length = 10

    for _ in range(total_cycles):
        for char in loading_chars:
            for _ in range(cycle_length):
                sys.stdout.write('\r正在查找大文件... ' + char)
                sys.stdout.flush()
                time.sleep(interval / cycle_length)
            sys.stdout.write('\r正在查找大文件... ' + loading_chars[0])
            sys.stdout.flush()

def filter_files(files, skip_file_keywords, include_file_keywords, extension=None):
    """根据文件名关键词和扩展名过滤文件列表"""
    filtered_files = [file for file in files if (not skip_file_keywords or all(keyword not in file for keyword in skip_file_keywords)) and
                      (not include_file_keywords or any(keyword in file for keyword in include_file_keywords))]
    if extension is not None:
        filtered_files = [file for file in filtered_files if file.endswith('.' + extension)]
    return filtered_files

def filter_dirs(dirs, skip_dir_keywords, include_dir_keywords):
    """根据目录名关键词过滤目录列表"""
    return [dir for dir in dirs if (not skip_dir_keywords or all(keyword not in dir for keyword in skip_dir_keywords)) and
             (not include_dir_keywords or any(keyword in dir for keyword in include_dir_keywords))]

def get_all_large_files_with_loading(dir_path, threshold_mb, skip_dir_keywords, skip_file_keywords, include_dir_keywords, include_file_keywords, extension=None, interval=0.5):
    """查找目录下所有大于指定大小的文件,同时跳过或仅包括特定关键词的文件夹及文件名称,并显示加载动画直到完成"""
    start_time = time.time()
    stop_event = Event()
    large_files = []
    loading_thread = threading.Thread(target=show_loading_animation, args=(stop_event, interval))
    loading_thread.daemon = True
    loading_thread.start()

    try:
        for root, dirs, files in os.walk(dir_path):
            dirs[:] = filter_dirs(dirs, skip_dir_keywords, include_dir_keywords)
            filtered_files = filter_files(files, skip_file_keywords, include_file_keywords, extension)
            for file in filtered_files:
                full_path = os.path.join(root, file)
                try:
                    if is_large(full_path, threshold_mb):
                        file_info = {'path': full_path, 'size': os.path.getsize(full_path) / 1024 / 1024}
                        large_files.append(file_info)
                except Exception as e:
                    print(f"警告访问文件出错 {full_path} 出错信息: {e}")

    finally:
        stop_event.set()
        loading_thread.join()
    large_files.sort(key=lambda x: x['size'], reverse=True)
    for file_info in large_files:
        print(f"文件路径: {file_info['path']} | 文件大小: {file_info['size']:.2f} MB")

    end_time = time.time()
    print(f"\n查找共耗时: {end_time - start_time:.2f} 秒")

def main():
    dir_path = input("请输入要检查的目录路径: ")
    try:
        threshold_mb = float(input("请输入文件大小阈值(单位: MB): "))
        skip_dir_keywords = input("请输入要跳过的文件夹名关键词,用逗号分隔(直接回车跳过,推荐modules,~~,.gradle): ").split(',')
        skip_file_keywords = input("请输入要跳过的文件名关键词,用逗号分隔(直接回车跳过,推荐$): ").split(',')
        include_dir_keywords = input("请输入要包含的文件夹名关键词,用逗号分隔(直接回车跳过): ").split(',')
        include_file_keywords = input("请输入要包含的文件名关键词,用逗号分隔(直接回车跳过): ").split(',')
        extension = input("请输入要筛选的文件扩展名(例如:txt,可选,直接回车跳过): ").strip('.') or None
        get_all_large_files_with_loading(dir_path, threshold_mb, skip_dir_keywords, skip_file_keywords, include_dir_keywords, include_file_keywords, extension)
        print("搜索结束.")
    except ValueError:
        print("错误:请输入有效的数字作为文件大小阈值.")
    except OSError as e:
        print(e)

if __name__ == '__main__':
    main()
相关推荐
zmzb01031 小时前
Python课后习题训练记录Day194
python·opencv·计算机视觉
benchmark_cc1 小时前
批量获取量化数据时,如何设置合理的超时和重试机制?——QuantDash 高性能实战指南
开发语言·人工智能·python·pandas·量化·quantdash
2601_966949651 小时前
使用 Pandas 读取批量数据时如何避免内存溢出?QuantDash 量化数据工程师避坑指南
开发语言·python·pandas·tushare·akshare·quantdash
65岁退休Coder1 小时前
LangChain v1.3.4 笔记 - 08 MCP & 相关概念
后端·python·langchain
wdloumiga2 小时前
使用 Construct 3零基础做一些2D小游戏
python·游戏·游戏2d
用户8356290780512 小时前
使用 Python 查找和替换 Word 文档中的文本
后端·python
黑马水牛3 小时前
Carla仿真系列:9_Carla 单目障碍物测距,四种方法原理与实测
经验分享·python·深度学习·计算机视觉·ros·传感器·carla仿真
Python私教3 小时前
多个项目怎么安全合并?适配层、双轨验证与可回滚切换
python·软件架构·系统迁移
船厂电气自动化ai大模型3 小时前
AI大模型与数学第42课:泰勒级数完整展开(神经网络近似核心)
人工智能·python·深度学习·算法·机器学习
刘新洲4 小时前
我以为 AI Agent 只是调模型,直到我亲手补上审批、Outbox 和故障恢复
python·agent·fastapi