开发者

How can I make an error verifiy with os.makedirs in Python?

How开发者_开发问答 can I make a verify error for this command?

if blablablabla:
    os.makedirs('C:\\test\\')

If the folder already exists, he return me an error... how can I make it ignore this error? and move on ?


try:
    os.makedirs('C:\\test\\')
except OSError:
    pass

You also might want to check the specific "already exists" error (since OSError could mean other things, like permission denied...

import errno
try:
    os.makedirs('C:\\test\\')
except OSError as e:
    if e.errno != errno.EEXIST:
        raise  # raises the error again


In Python3.2 and above, just add exist_ok=True will solve this problem.

If exist_ok is False (the default), an FileExistsError is raised if the target directory already exists.

os.makedirs('C:\\test\\',exist_ok=True)


you can try/except?

try:
   os.makedirs('C:\\test\\')
except: pass
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜