How do I continue after the error?

chad cdalten at gmail.com
Thu Jun 4 23:10:50 EDT 2009


On Jun 4, 7:36 pm, Steven D'Aprano
<ste... at REMOVE.THIS.cybersource.com.au> wrote:
> 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.
>

Like what? All the program is meant to do is scroll ASCII art in a
chat room.




More information about the Python-list mailing list