Paramiko的SFTPClient显然没有exists
方法。这是我当前的实现:
def rexists(sftp, path):
"""os.path.exists for paramiko's SCP object
"""
try:
sftp.stat(path)
except IOError, e:
if 'No such file' in str(e):
return False
raise
else:
return True
有更好的办法吗?检查异常消息中的子字符串相当难看,而且可能不可靠。
Tags:
没有为SFTP定义“exists”方法(不仅仅是paramiko),因此您的方法很好。
我觉得检查一下错误有点干净:
有关定义所有这些错误代码的常量,请参见^{} module 。此外,使用异常的
errno
属性比使用__init__
参数的扩展更清楚一些,因此我将执行以下操作:Paramiko字面上提升了
FileNotFoundError
相关问题 更多 >
编程相关推荐