我在这个论坛上遇到过这样的例子:通过测试OSError
(或者现在的IOError
)中的errno
值来处理文件和目录的特定错误。例如,这里的一些讨论-Python's "open()" throws different errors for "file not found" - how to handle both exceptions?。但是,我认为,这不是正确的方式。毕竟,FileExistsError
的存在是为了避免担心errno
。
由于令牌FileExistsError
出现错误,以下尝试不起作用。
try:
os.mkdir(folderPath)
except FileExistsError:
print 'Directory not created.'
如何具体检查此错误和类似的其他错误?
下面是一个在尝试atomically overwrite an existing symlink时处理竞争条件的示例:
根据代码} 是在Python 3.3中添加的;您不能使用
print ...
,您似乎正在使用Python 2.x.^{FileExistsError
。使用^{} :
相关问题 更多 >
编程相关推荐