简体   繁体   English

Python异常 - 循环中具有try ... except子句的函数

[英]Python exceptions - a function with try…except clause in a loop

I frequently encounter a situation like this: 我经常遇到这样的情况:

import os

for i in range(10):
    os.mkdir(i)

However, sometimes a directory already exists, in which case os.mkdir throws an OSError. 但是,有时目录已经存在,在这种情况下os.mkdir会引发OSError。 When this happens, I just want it to skip the rest of the loop and move to the next iteration of i, so I often write something like this: 当发生这种情况时,我只是希望它跳过循环的其余部分并移动到i的下一次迭代,所以我经常写这样的东西:

for i in range(10):
    try:
        os.mkdir(i)
    except OSError:
        continue

However, what I really, really want is a function which encapsulates that behaviour. 但是,我真正想要的是一个封装该行为的函数。 Something like this: 像这样的东西:

def custom_mkdir(directory):
    try:
        os.mkdir(directory)
    except OSError:
        continue

So that I can have some code like this: 这样我可以有一些像这样的代码:

for i in range(10):
    custom_mkdir(i)

with the intended behaviour that it makes the directory if it doesn't exist but skips to the next interation of the for loop if it does. 具有预期的行为,如果目录不存在,它会跳转到for循环的下一个交互,如果它存在的话。

However, the continue statement can't be included in a function in that way. 但是,continue语句不能以这种方式包含在函数中。 So how do I get the intended behaviour without resorting to: 因此,如何不采取以下措施来获得预期的行为:

for i in range(10):
    try:
        custom_mkdir(i)
    except OSError:
        continue

which is an even worse situation than the first? 哪个比第一个更糟?

Well, if there's nothing else going on inside the loop you can just do this: 好吧,如果循环内部没有其他内容,你可以这样做:

def custom_mkdir(directory):
    try:
        os.mkdir(directory)
    except OSError:
        pass

for i in range(10):
    custom_mkdir(i)

If there's actually more code below custom_mkdir , I think the best you can do is this: 如果在custom_mkdir下面实际上有更多代码,我认为你能做的最好的是:

def custom_mkdir(directory):
    try:
        os.mkdir(directory)
        return True
    except OSError:
        return False

for i in range(10):
    if not custom_mkdir(i):
       continue

or 要么

for i in range(10):
    if custom_mkdir(i):
        # The rest of the logic in here

Which are at least a little more concise than the original. 至少比原作简洁一点。

When OSError is caught inside custom_mkdir , what your function really wants to do is just do nothing. OSError被捕获在custom_mkdir ,您的函数真正想要做的就是什么都不做。 So, do nothing: 因此,什么都不做:

def custom_mkdir(directory):
    try:
        os.mkdir(directory)
    except OSError:
        pass

Just return a bool: 回来一个布尔:

def custom_mkdir(directory):
    try:
        os.mkdir(directory)            
    except OSError:
        return False
    return True

for i in range(10):
    if not custom_mkdir(directory): continue

code: 码:

def cusyom_mkdir(diectory):
try:
    os.mkdir(directory)
    return True
except OSError:
    return False

status = map(cusyom_mkdir, [i for i in range(10)]) status = map(cusyom_mkdir,[i for i in range(10)])

or 要么

def cusyom_mkdir(diectory):
try:
    os.mkdir(directory)
    return True, directory
except OSError:
    return False, directory

status = map(cusyom_mkdir, [i for i in range(10)]) status = map(cusyom_mkdir,[i for i in range(10)])

You can see that the directory is successfully created the directory creation fails 您可以看到目录创建失败,目录创建失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM