How do I continue after the error?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Fri Jun 5 04:36:13 CEST 2009


On Thu, 04 Jun 2009 18:26:49 -0700, chad wrote:

> Let's say I have a list of 5 files. Now lets say that one of the files
> reads nude333.txt instead of nude3.txt. When this happens, the program
> generates an error and quits. What I want it to do is just skip over the
> bad file and continue on with the next one.


This should give you some hints.


import errno

for name in list_of_file_names:
    try:
        f = open(name, 'r')
    except IOError, e:
        if e.errno == errno.ENOENT:  # or just use 2 if you're lazy
            # no such file, skip
            continue
        # some other more serious error, re-raise the exception
        raise
    do_something_with(f)


[...]
> def scroll_some_porn(now):
>     while 1:
>         for some_titty_porn in my_porn_collection:
>             now.write(" \n")
>             bitch_please = generate(some_titty_porn)
>             now.write(bitch_please)
>             time.sleep(10)
>             now.write(" \n")

Shouldn't you have some way of escaping from the infinite loop other than 
typing Ctrl-C at the console? I imagine your hands will be busy.



-- 
Steven



More information about the Python-list mailing list