python - 检查文件夹大小是否大于特定大小的最快方法是什么?

标签 python linux shell

检查文件夹大小是否超过特定大小(例如 10 MB、1 Gb、10 GB 等)而不实际计算文件夹大小的最快方法是什么。配额之类的东西。 Pythonic 解决方案会很棒,但也欢迎使用标准 UNIX 实用程序

最佳答案

import os
from os.path import join, getsize

def getsize_limited(directory, limit):
        total_size = 0
        for root, dirs, files in os.walk(directory, topdown=False):
            for name in files:
                total_size += getsize(join(root, name))
                if total_size > limit:
                   return limit, False
        return total_size, True

例子:

size, within_limit = getsize_limited(os.getcwd(), limit=10**6)

关于python - 检查文件夹大小是否大于特定大小的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2414917/

相关文章:

linux - 在压缩文件上使用 sed

python - 外部库使用 print 而不是 return

c++ - "' omp.h ' file not found"使用 Clang 编译时

linux - 如何在Linux shell脚本中将文本文件中的两行合并到同一行中

c - Linux 内核中 FPU 的使用

linux - 将选定的文本发送到节日

python - 在 Django shell session 期间获取 SQL 查询计数

python - 如何在 Python OpenCV 中绘制图像对数色度图

python - 将平均函数应用于具有外部条件的 MultiIndex 数据帧

python - 将 timedelta 的时间转换/使用到日期时间