简体   繁体   中英

How to stop a while loop in the middle of its run and go back to start?

My program that writes a file every 10 minutes, and then uses PIL to manipulate it. However, due to the old computer and slow external hard disk, it sometimes doesn't create a new file. So PIL just uses the old file.

I want to write a script that checks to make sure a new file has been created based on its file size and then if it's not the size it's supposed to be, the loop stops before the PIL part, and restarts the program from the top immediately.

So it would look something like this:

while True:
    #task A that saves a new file

      # new script to check the file size
        if correct, continue to task B
        if incorrect, break and restart

    #task B PIL image manipulation 

You're looking for the continue statement:

while True:
    #task A that saves new file

    if we_should_go_to_task_B:
        pass # This does nothing and the loop carries on
    else:
        continue # This goes back to the start of the loop

    #task B PIL image manipulation 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM