【发布时间】:2021-07-01 05:00:15
【问题描述】:
我对 python 很陌生,我正在尝试弄清楚如何检查文件夹中图片的大小。之后我只想在文件大小大于 10kb 时继续。
【问题讨论】:
标签: python
我对 python 很陌生,我正在尝试弄清楚如何检查文件夹中图片的大小。之后我只想在文件大小大于 10kb 时继续。
【问题讨论】:
标签: python
你可以使用 os 模块
import os
b = os.path.getsize("filename")
print(b)
大小将以字节为单位
【讨论】:
你可以试试关注
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
【讨论】: