输出指定目录下 所有子目录大小

输出指定目录下( D: ) 所有子目录体积占用 并格式化输出txt

在这里插入图片描述

在这里插入图片描述

计算133889个文件夹里的存储占用 多线程版本耗时103秒

Python版本

3.9

文件类型

exe

运行环境

window

CSDN

https://download.csdn.net/download/qq_39162566/86985328

百度连接

链接:https://pan.baidu.com/s/1ImM8a5fufPc6HgPSbx5g1g
提取码:7777

蓝奏云

https://wwf.lanzoue.com/iojCi0ft7rcb

Github

https://github.com/badApple001/pythonTool

源码

from os import path, walk, listdir, getcwd
import time
import threading


home_path = input("root 目录:\n")
if not path.isdir(home_path):
    raise Exception(f"{home_path} 不是一个文件夹")

print("正在递归遍历所有文件大小 时间有点长 去喝杯茶吧...")
start_time = time.time()

dirs = listdir(home_path)
details = []
threads = []
# R = threading.Lock()

def get_file_totalSize(root_path_url,root_info_data):
    total_size = 0
    root_level = root_path_url.count('\\')
    sub_dir_datas = root_info_data[2]
    for dirpath, dirnames, filenames in walk(root_path_url):
        size = sum([path.getsize(path.join(dirpath, f))
                          for f in filenames])
        cur_level = dirpath.count('\\') - root_level
        total_size += size
        if dirpath != root_path_url:
            sub_dir_datas.append([dirpath,size,cur_level])
            # info = [dirpath,size,cur_level]
            # #加锁
            # R.acquire()
            # if None != dirdict[dirpath]:
            #     dirdict[dirpath][2].append(info)
            # else:
            #     dirdict[dirpath] = info
            # #解锁
            # R.release()
    #加锁
    # R.acquire()
    root_info_data[1] = total_size
    #解锁
    # R.release()
for f in dirs:
    dir = path.join(home_path,f)
    if path.isdir(dir):
        dirroot = [dir,0,[]]
        details.append(dirroot)
        t1 = threading.Thread(target=get_file_totalSize,args=(dir,dirroot))
        threads.append(t1)
        t1.start()
# 这里类似await  只有所有线程执行完成后 才能继续执行后面的代码
for t in threads:
    t.join()

print(f"目录大小已统计完成 正在对齐结果排序...")
sortedList = []
if len(details) > 0:
    details.sort(key=lambda x: x[1],reverse=True)

print(f"子目录大小排序结果...")
for f in details:
    sizeContent = f"{f[1]/float(1024 * 1024)}MB" if f[1] > 1024 * 1024 else f"{f[1] / float(1024)}KB" if f[1] > 1024 else f"{f[1]}B"
    print(f"{f[0]}\t{sizeContent}")

print("-------------------------------")
print("正在将目录结构及占用详情写入本地txt文件中...")
if len(details) > 0:
    details_folder = path.join(getcwd(),"目录存储详情.txt")
    content = ""

    for f in details:
        sizeContent = f"{f[1]/float(1024 * 1024)}MB" if f[1] > 1024 * 1024 else f"{f[1] / float(1024)}KB" if f[1] > 1024 else f"{f[1]}B"
        lineContent = f"{f[0]} ({sizeContent})\n"
        content += lineContent
        
        for child in f[2]:
            sizeContent = f"{child[1]/float(1024 * 1024)}MB" if child[1] > 1024 * 1024 else f"{child[1] / float(1024)}KB" if child[1] > 1024 else f"{child[1]}B"
            lineContent = f"{child[0]} ({sizeContent})\n"
            for i in range(0,child[2],1):
                content += '\t'
            content += lineContent

    with open(details_folder,'w',encoding="utf-8") as f:
        f.write(content)
    print(f"文件地址: {details_folder}")


print("----------程序运行结束----------")
print(f"运行用时: {time.time()-start_time}秒")
input("任意键退出")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值