如何在 Python 中使用 os.makedirs 进行错误验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2383816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make an error verifiy with os.makedirs in Python?
提问by Bruno 'Shady'
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 ?
如果文件夹已经存在,他会返回一个错误...我怎样才能让它忽略这个错误?继续前进?
回答by nosklo
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...
您可能还想检查特定的“已存在”错误(因为OSError
可能意味着其他事情,例如权限被拒绝......
import errno
try:
os.makedirs('C:\test\')
except OSError as e:
if e.errno != errno.EEXIST:
raise # raises the error again
回答by ghostdog74
you can try/except?
你可以试试/除了?
try:
os.makedirs('C:\test\')
except: pass