I am pretty new to python and I am trying to figure out how to check the sizes of pictures I have in a folder. After that I want to proceed only if the filesize is larger than 10kb.
Asked
Active
Viewed 48 times
2 Answers
1
you can use os module
import os
b = os.path.getsize("filename")
print(b)
The size will be in bytes

Kritarth
- 48
- 11
-
But this would require me to enter each file name individually if I am not mistaken. What I want is to enter the folder with the pictures, loop over them and give me the size of each picture. – Alexander Apr 05 '21 at 10:39
-
https://stackoverflow.com/questions/1507084/how-to-check-dimensions-of-all-images-in-a-directory-using-python maybe this will help – Kritarth Apr 05 '21 at 10:42
0
You can try following
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

Harish
- 789
- 1
- 7
- 21