Rei*_*ica 14 python ssh scp paramiko
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
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?在异常消息中检查子字符串非常难看,并且可能不可靠.
Mat*_*ood 18
有关定义所有这些错误代码的常量,请参阅errno模块.此外,使用errno异常的属性比__init__args 的扩展更清楚,所以我这样做:
except IOError, e: # or "as" if you're using Python 3.0
if e.errno == errno.ENOENT:
...
Run Code Online (Sandbox Code Playgroud)
Paramiko 字面上提出 FileNotFoundError
def sftp_exists(sftp, path):
try:
sftp.stat(path)
return True
except FileNotFoundError:
return False
Run Code Online (Sandbox Code Playgroud)
没有为SFTP(不仅仅是paramiko)定义"存在"方法,所以你的方法很好.
我认为检查errno有点清洁:
def rexists(sftp, path):
"""os.path.exists for paramiko's SCP object
"""
try:
sftp.stat(path)
except IOError, e:
if e[0] == 2:
return False
raise
else:
return True
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17423 次 |
| 最近记录: |