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.
So, I have:-
for dirname in listofdirs:
try:
os.mkdir(dirname)
except FileExistsError:
# so what can I do here that says 'carry on regardless'
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'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.
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)
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)
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
On Sun, 30 Apr 2023 at 11:58, Chris Green <cl@isbd.net> wrote:
Chris Angelico <rosuav@gmail.com> wrote:wrote:
On Sat, 29 Apr 2023 at 14:27, Kushal Kumaran <kushal@locationd.net>
elegantOn 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
it'sway to do this. I want to create a directory, but if it exists
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.
os.mkdir()I suppose I could test if the directory exists before the
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
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 11:00:38 |
Calls: | 10,387 |
Calls today: | 2 |
Files: | 14,060 |
Messages: | 6,416,692 |