Mailing List Archive

How to 'ignore' an error in Python?
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.


--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
On 4/28/23 09:55, Chris Green 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.

For this specific case, you can use os.makedirs:

os.makedirs(dirname, exist_ok=True)

The mkdir in pathlib also takes the exist_ok flag


As to the way you asked the question, you can use "pass" or the ellipses
for the "# so what can I do here"


--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
On 2023-04-28 16:55, Chris Green 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.
>
I'd do this:

from contextlib import suppress

for dirname in listofdirs:
with suppress(FileExistsError):
os.mkdir(dirname)

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
On 4/28/23 11:05, MRAB wrote:
> On 2023-04-28 16:55, Chris Green 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'd do this:
>
>     from contextlib import suppress
>
>     for dirname in listofdirs:
>         with suppress(FileExistsError):
>             os.mkdir(dirname)

I'm fond of that approach too, though you can't use if it you really
wanted to do the

except:
# handle any other error, which is really an error

If you're okay letting Python just raise whatever other error it found,
then great!


--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
On 28Apr2023 10:39, Mats Wichmann <mats@wichmann.us> wrote:
>For this specific case, you can use os.makedirs:
>os.makedirs(dirname, exist_ok=True)

I'm not a great fan of makedirs because it will make all the missing
components, not just the final one. So as an example, if you've got a
NAS mounted backup area at eg:

/mnt/nas/backups/the-thing/backups-subdir

and your config file has this mistyped as:

/mn/nas/backups/the-thing/backups-subdir

and makedirs is used, then it will make the backup area on eg the root
drive where there's no room. (I'm looking at you, Docker, grr). There
are plenty of similar situations.

Because of this I usually am prepared to make a missing final component
with mkdir(), but not a potentially deep path with makedirs().

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
On 28Apr2023 16:55, Chris Green <cl@isbd.net> wrote:
> 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.

You don't. Provided the "handle any other error" part reraises or does a
break/continue, so as to skip the bottom of the loop body.

ok = true
for dirname in listofdirs:
try:
os.mkdir(dirname)
except FileExistsError:
pass
except Exception as e:
warning("mkdir(%r): %s", dirname, e)
ok = False
continue
rest of the loop body ...
if not ok:
... not all directories made ...

2 notes on the above:
- catching Exception, not a bare except (which catches a rather broader
suit of things)
- reporting the other exception

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green <cl@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
Re: How to 'ignore' an error in Python? [ In reply to ]
On Sat, 29 Apr 2023 at 14:27, Kushal Kumaran <kushal@locationd.net> wrote:
>
> On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green <cl@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.
>

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:

try: os.mkdir(dirname)
except FileExistsError: pass

and not try to handle anything else at all.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
Kushal Kumaran <kushal@locationd.net> wrote:
> On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green <cl@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.
>
It was rather using os.mekedirs() to create a single directory that
seemed wrong. If os.mkdir() had exist_ok=True than that would have
been the obvious way to do it.

--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
Chris Angelico <rosuav@gmail.com> wrote:
> On Sat, 29 Apr 2023 at 14:27, Kushal Kumaran <kushal@locationd.net> wrote:
> >
> > On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green <cl@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.
> >
>
> 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:
>
> try: os.mkdir(dirname)
> except FileExistsError: pass
>
> and not try to handle anything else at all.
>
Yes, OP here, that seems to me to be the 'right' way to do it.
Basically I hadn't realised the effect of pass in a try block and
that's why I asked the question originally.

--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
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.

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
Stefan Ram ha scritto:
> jak <nospam@please.ty> writes:
>> 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.
>
> If the name exists and it is a file's name, this will be detected
> by "mkdir( exist_ok=True )", and an object will be raised. Such
> an object, indeed, usually should /not/ be ignored by the caller.
> But there is no reason to deviate from the EAFP path.
>
>


Maybe I expressed myself badly but I didn't mean to propose alternatives
to the EAFP way but just to evaluate the possibility that it is not a
folder.
--
https://mail.python.org/mailman/listinfo/python-list
RE: How to 'ignore' an error in Python? [ In reply to ]
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@python.org> On
Behalf Of Kushal Kumaran
Sent: Saturday, April 29, 2023 12:19 AM
To: python-list@python.org
Subject: Re: How to 'ignore' an error in Python?

On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green <cl@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

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
On 30/04/23 2:43 am, jak wrote:
> Maybe I expressed myself badly but I didn't mean to propose alternatives
> to the EAFP way but just to evaluate the possibility that it is not a
> folder.

If it's not a folder, you'll find out when the next thing you
try to do to it fails.

You could check for it earlier, but there's still the possibility
of a race condition -- someone could delete the folder and replace
it with a file in the meantime. Or just delete it and not replace
it with anything. So you need to be prepared to deal with failures
at any point.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
On Sun, 30 Apr 2023 at 11:58, Chris Green <cl@isbd.net> wrote:
>
> Chris Angelico <rosuav@gmail.com> wrote:
> > On Sat, 29 Apr 2023 at 14:27, Kushal Kumaran <kushal@locationd.net> wrote:
> > >
> > > On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green <cl@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.
> > >
> >
> > 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:
> >
> > try: os.mkdir(dirname)
> > except FileExistsError: pass
> >
> > and not try to handle anything else at all.
> >
> Yes, OP here, that seems to me to be the 'right' way to do it.
> Basically I hadn't realised the effect of pass in a try block and
> that's why I asked the question originally.
>

There's two points to note here. "pass" doesn't do anything
whatsoever; it's only there to prevent the syntactic problem of having
nothing in that block. This will also suppress the error:

try:
os.mkdir(dirname)
except FileExistsError:
dummy = "ignore"

The second thing is that, as soon as you have an "except" clause that
matches the error, that's it - it stops there. The error is considered
handled at that point. If that's NOT what you want, you have a few
options. Firstly, you can simply not have a matching except clause.
That's why we like to be as precise as possible with our catching;
every other type of problem will be left uncaught. Secondly, you can
use "try/finally" to add code that happens as the exception flies by,
but doesn't catch it (it also happens at the end of the block for
other reasons). And thirdly, you can reraise the exception:

try:
os.mkdir(dirname)
except FileExistsError:
print("Hey, that one already exists!")
raise

That's going to keep the exception going just as if it hadn't been
caught, but with the additional handling.

But if you don't do any of those things, the exception is deemed to be
handled, and it goes no further.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
On Sun, 30 Apr 2023 at 12:02, jak <nospam@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
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python? [ In reply to ]
Unsubscribe

On Sat, Apr 29, 2023 at 7:05 PM Chris Angelico <rosuav@gmail.com> wrote:

> On Sun, 30 Apr 2023 at 11:58, Chris Green <cl@isbd.net> wrote:
> >
> > Chris Angelico <rosuav@gmail.com> wrote:
> > > On Sat, 29 Apr 2023 at 14:27, Kushal Kumaran <kushal@locationd.net>
> wrote:
> > > >
> > > > On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green <cl@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.
> > > >
> > >
> > > 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:
> > >
> > > try: os.mkdir(dirname)
> > > except FileExistsError: pass
> > >
> > > and not try to handle anything else at all.
> > >
> > Yes, OP here, that seems to me to be the 'right' way to do it.
> > Basically I hadn't realised the effect of pass in a try block and
> > that's why I asked the question originally.
> >
>
> There's two points to note here. "pass" doesn't do anything
> whatsoever; it's only there to prevent the syntactic problem of having
> nothing in that block. This will also suppress the error:
>
> try:
> os.mkdir(dirname)
> except FileExistsError:
> dummy = "ignore"
>
> The second thing is that, as soon as you have an "except" clause that
> matches the error, that's it - it stops there. The error is considered
> handled at that point. If that's NOT what you want, you have a few
> options. Firstly, you can simply not have a matching except clause.
> That's why we like to be as precise as possible with our catching;
> every other type of problem will be left uncaught. Secondly, you can
> use "try/finally" to add code that happens as the exception flies by,
> but doesn't catch it (it also happens at the end of the block for
> other reasons). And thirdly, you can reraise the exception:
>
> try:
> os.mkdir(dirname)
> except FileExistsError:
> print("Hey, that one already exists!")
> raise
>
> That's going to keep the exception going just as if it hadn't been
> caught, but with the additional handling.
>
> But if you don't do any of those things, the exception is deemed to be
> handled, and it goes no further.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list