IOError and Try Again to loop the loop.

Chris Rebert clp2 at rebertia.com
Sun Jul 11 22:31:00 EDT 2010


On Sun, Jul 11, 2010 at 7:15 PM, The Danny Bos <dannybos at gmail.com> wrote:
> Heya,
>
> I'm running a py script that simply grabs an image, creates a
> thumbnail and uploads it to s3. I'm simply logging into ssh and
> running the script through Terminal. It works fine, but gives me an
> IOError every now and then.
>
> I was wondering if I can catch this error and just get the script to
> start again?
> I mean, in Terminal it dies anyway, so I have to start it again by
> hand, which is a pain as it dies so sporadically. Can I automate this
> error, catch it and just get it to restart the loop?

Of course. Use try-except;
http://docs.python.org/tutorial/errors.html#handling-exceptions
Here's one way to do it:

for image in images:#or whatever the actual loop is
    # whatever
    while True:
        try:
            part_that_may_raise_IOError()
        except IOError:
            print("IOError; Retrying...")
        else:
            break
    # whatever other stuff

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list