0

I am trying to validate folder size to be less than 2 MB, from a output of 500K / 1M / 1.5M how can i validate if folder_size is less than 2M (i.e 2MB)

sample output:

folder_size = 1M;

folder_size = 500K

This is how I want to validate

if folder_size <= 2M
    print("pass")
else
    print("fail")
iltech
  • 183
  • 1
  • 2
  • 11
  • 1
    You'll need to parse the suffixed number into an integer (such as `M` meaning 1024 * 1024), and then check against your thresholds. – AKX May 17 '23 at 10:02
  • Refer to [this answer](https://stackoverflow.com/a/51253225/6699447). It describes how you can convert your size notation with units to number of bytes. Then do the comparison based on bytes. – Abdul Niyas P M May 17 '23 at 10:04
  • how can we differentiate if the folder size is in KB? if we multiply 1024 * prefix (say 500Kb), we would be doing 500 * 1024 = this would not what we expect correct? – iltech May 17 '23 at 10:06
  • @AbdulNiyasPM I checked the answer you posted, that I am not sure how I can implement in my script since I get value as 500K or 1M or 1.1M. How can I multiply * 1024 – iltech May 17 '23 at 10:17
  • https://stackoverflow.com/questions/1392413/calculating-a-directorys-size-using-python might be of use – pinegulf May 17 '23 at 12:10

0 Answers0