0

The file download works well with individual files but when I run it in a for loop to download multiple files at once, it keeps crashing after downloading the first file. I tried multiple things but the result is same, the program stops after the first download with no error.

def store_files_name(fname):
    file_names.append(fname) 

def store_dir_name(dirname):
    dir_names.append(dirname)

def store_other_file_types(name):
    un_name.append(name)
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

conn = pysftp.Connection('', username='', password='', private_key=".ppk", cnopts=cnopts)
sftp.walktree("",store_files_name,store_dir_name,store_other_file_types,recurse=True)



for f in file_names:
    print(f)
    conn.get("/"+f)

1 Answers1

0

All pysftp recursive functions are known to be poorly implemented. They particularly do not work on Windows. They use os.sep and os.path functions for remote SFTP paths, what is wrong, as SFTP paths always use a forward slash.

And in general pysftp seems to be an abandoned project. Consider using Paramiko directly instead (pysftp is just a thin wrapper around Paramiko).

For a working Paramiko code that iterates a remote folder, see:
Python pysftp get_r from Linux works fine on Linux but not on Windows

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992