Running the same program until condition satisfied

  function, python, while-loop

I am trying to create a small program that searches a folder of images, chooses one, checks its size and finishes if the chosen image is at least 5KB. If it is not then I need it to loop back to the choosing step (and then the size check, and so on..)

I am using functions for the choosing and the size-check but when I try to use them in a while loop I get all sorts of indentation errors and now I’m very confused. I’ve commented the section where I was using the function, but really I guess I want the whole thing to loop back, to the comment at the top..
Here’s my code –

#CHOOSE POINT
def chosen():
    random.choice(os.listdir(r"/Users/me/p1/images"))

def size():
    os.path.getsize(r"/Users/me/p1/images/"+chosen)

thresh = 5000
while size < thresh:
    print(chosen + " is too small")
    # loop back to CHOOSE POINT
else:
    print(chosen + " is at least 5KB")

Am I thinking about this all wrong? Will using the function in my while-loop do what I want? What’s the best way to achieve what I’m trying to do? I’m quite new to this and getting very confused.

Source: Python Questions

LEAVE A COMMENT