How to 'ignore' an error in Python?

avi.e.gross at gmail.com avi.e.gross at gmail.com
Sat Apr 29 13:41:41 EDT 2023


I get a tad suspicious when someone keeps telling us every offered solution
does not feel right. Perhaps they are not using the right programming
language as clearly they are not willing to work with it as it is not as it
should be.

 After all the back and forth, there are several choices including accepting
whatever method is least annoying to them, or rolling their own.

Create a function with a name like do_it_my_way_or_the_highway() that uses
any acceptable method but completely hides the implementation details from
your code. One way might be to get the source code and copy it under your
own name and modify it so the default is to do what you want. It can even be
as simple as a small wrapper that forwards to the original function with a
keyword set by default.

After all, this does seem to be a bit like what you are asking for. A way to
call your functionality that does it the way you insist it should have been
designed, never mind that many others are happy with it as it is and use the
techniques mentioned at other times.

But I do have sympathy. I have seen lots of simple-minded code that seems to
cleanly and elegantly solve a problem as long as all the ducks are just-so.
Then someone points out that the code may break if it is called with some
other type than expected or if it tries to divide by zero or if something
else changes a variable between the time you looked at it and the time you
update it and so on. Next thing you know, your code grows (even
exponentially) to try to handle all these conditions and includes lots of
nested IF statements and all kinds of TRY statements and slows down and is
hard to read or even think about. And to make it worse, people ask for your
formerly simple function to become a Swiss army knife that accepts oodles of
keyword arguments that alter various aspects of the behavior!

So, yes, it can feel wrong. But so what? Sometimes you can find ways to
reduce the complexity and sometimes you simply create a few accessory
functions you can use that tame the complexity a bit. But almost any complex
program in any language can require a loss of simplicity.


-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of Kushal Kumaran
Sent: Saturday, April 29, 2023 12:19 AM
To: python-list at python.org
Subject: Re: How to 'ignore' an error in Python?

On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green <cl at isbd.net> wrote:
> I'm sure I'm missing something obvious here but I can't see an elegant
> way to do this.  I want to create a directory, but if it exists it's
> not an error and the code should just continue.
>
> So, I have:-
>
>     for dirname in listofdirs:
>         try:
>             os.mkdir(dirname)
>         except FileExistsError:
>             # so what can I do here that says 'carry on regardless'
>         except:
>             # handle any other error, which is really an error
>
>         # I want code here to execute whether or not dirname exists
>
>
> Do I really have to use a finally: block?  It feels rather clumsy.
>
> I suppose I could test if the directory exists before the os.mkdir()
> but again that feels a bit clumsy somehow.
>
> I suppose also I could use os.mkdirs() with exist_ok=True but again
> that feels vaguely wrong somehow.
>

Why does exist_ok=True feel wrong to you?  This is exactly what it is
there for.

-- 
regards,
kushal
-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list