How to 'ignore' an error in Python?

Chris Angelico rosuav at gmail.com
Sat Apr 29 22:05:49 EDT 2023


On Sun, 30 Apr 2023 at 12:02, jak <nospam at please.ty> wrote:
>
> Chris Angelico ha scritto:
> > Using mkdirs when you only want to make one is inviting problems of
> > being subtly wrong, where it creates too many levels of directory.
> > Personally, I would just do:
>
>
> Maybe I only say this because it has happened to me too many times but
> before ignoring the error in the 'except' branch, I would make sure that
> if the name exists it is a folder and not a file.
>

That's a fair consideration, although the other way to handle that is
to allow other operations to fail later (if someone creates a file
called "logs" and then you try to create "logs/2023-04-30.txt", you
get an error at that point). I have also known situations where this
is a deliberate way to suppress something (like a cache or log
directory).

ChrisA


More information about the Python-list mailing list