使用Paramiko递归目录下载? [英] Recursive directory download with Paramiko?
本文介绍了使用Paramiko递归目录下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过 SSH 递归下载一个内容未知的目录,并且一直在尝试 Paramiko.我看过几个如何上传目录的例子,但没有一个涉及递归下载.
I want to download a directory with unknown contents recursively via SSH and have been trying Paramiko. I have seen several examples how to upload directories but none that covers recursive download.
我可以列出目录中的所有项目,但无法确定该项目是文件(下载)还是目录(递归调用).
I can list all items in a directory but haven't been able to find a way of knowing if the item is a file (to download) or a directory (to call recursively).
transport = paramiko.Transport((MY_IP, 22))
transport.connect(username=MY_NAME, password=MY_PASS)
sftp = paramiko.SFTPClient.from_transport(transport)
file_list = sftp.listdir(path='/home/MY_HOME_DIR')
for item in file_list:
# Here is an item name... but is it a file or directory?
print(item)
sftp.close()
transport.close()
那么我怎么知道一个项目是一个文件还是一个目录?
So how do I know if an item is a file or if it is a directory?
推荐答案
from stat import S_ISDIR
def isdir(path):
try:
return S_ISDIR(sftp.stat(path).st_mode)
except IOError:
#Path does not exist, so by definition not a directory
return False
...假设 sftp
是一个开放的 Paramiko SFTP 连接.
...assuming sftp
is an open Paramiko SFTP connection.
这篇关于使用Paramiko递归目录下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文