帕拉米科的SFTP客户端 http://www.metasnark.com/paramiko/docs/paramiko.SFTP-class.html显然没有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
有一个更好的方法吗?检查异常消息中的子字符串非常难看并且可能不可靠。
See the errno module http://docs.python.org/library/errno.html#errno.ENOENT用于定义所有这些错误代码的常量。另外,使用errno
异常的属性比扩展的__init__
args,所以我会这样做:
except IOError, e: # or "as" if you're using Python 3.0
if e.errno == errno.ENOENT:
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)