【问题标题】:How to get sizes of files in a folder如何获取文件夹中文件的大小
【发布时间】:2021-07-01 05:00:15
【问题描述】:

我对 python 很陌生,我正在尝试弄清楚如何检查文件夹中图片的大小。之后我只想在文件大小大于 10kb 时继续。

【问题讨论】:

    标签: python


    【解决方案1】:

    你可以使用 os 模块

    import os
    b = os.path.getsize("filename")
    print(b)
    

    大小将以字节为单位

    【讨论】:

    • 但是如果我没记错的话,这需要我单独输入每个文件名。我想要的是进入包含图片的文件夹,循环它们并给我每张图片的大小。
    • stackoverflow.com/questions/1507084/… 也许这会有所帮助
    【解决方案2】:

    你可以试试关注

    import os
    
    
    def get_file_size(directory):
    """Returns the `file` size in KB."""
    try:
        # print("[+] Getting the size of", file)
        for entry in os.scandir(directory):
            if entry.is_file():
                # if it's a file, use stat() function
                total = int('{:,.0f}'.format(entry.stat().st_size / float(1 << 10)))  #file size in KB
                if total > 10:
                    print("This file is greater that 10KB do something ")
    except PermissionError:
        # if for whatever reason we can't open the folder, return 0
        return 0
    return total
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多