使用pysftp递归下载 [英] Recursive download with pysftp

查看:67
本文介绍了使用pysftp递归下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下结构从 SFTP 获取数据:

I'm trying to fetch from SFTP with the following structure:

main_dir/
 dir1/
  file1
 dir2/
  file2

我尝试使用以下命令实现此目的:

I tried to achieve this with commands below:

sftp.get_r(main_path + dirpath, local_path)

sftp.get_d(main_path + dirpath, local_path)

本地路径类似d:/grabbed_files/target_dir,远程路径类似/data/some_dir/target_dir.

The local path is like d:/grabbed_files/target_dir, and the remote is like /data/some_dir/target_dir.

使用 get_r 我收到 FileNotFound 异常.使用 get_d 我得到空目录(当目标目录的文件不是目录时,它工作正常).

With get_r I am getting FileNotFound exception. With get_d I am getting empty dir (when target dir have files not dirs, it works fine).

我完全确定该目录存在于该路径中.我做错了什么?

I'm totally sure that directory exists at this path. What am I doing wrong?

推荐答案

我不明白为什么它不起作用,所以我以我自己的递归解决方案结束:

I didn't understand why it doesn't work so I ended with my own recursive solution:

def grab_dir_rec(sftp, dirpath):
    local_path = target_path + dirpath
    full_path = main_path + dirpath
    if not sftp.exists(full_path):
        return
    if not os.path.exists(local_path):
        os.makedirs(local_path)

    dirlist = sftp.listdir(remotepath=full_path)
    for i in dirlist:
        if sftp.isdir(full_path + '/' + i):
            grab_dir_rec(sftp, dirpath + '/' + i)
        else:
            grab_file(sftp, dirpath + '/' + i)

这篇关于使用pysftp递归下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆