0

I am new into python and i am trying to download file/folder(s) from a SFTP server using pysftp library. I am looking for recursive download so i am using pysftp.get_r() method.

Below are the cases:

1) root folder contains only folders : Working as expected. root folder

2) root folder contains folders and file: giving error stating FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Tools/DownloadedFromPython\\./' root folder Below is the code which i have written

import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys.load('C:/Users/xxx/.ssh/known_hosts')
lPath = 'C:/Tools/DownloadedFromPython'
sPath = '/'
sftp = pysftp.Connection(host='hostname', username='xxx', password='xxx', cnopts=cnopts)
if not os.path.exists(lPath):
    os.makedirs(lPath)
if sftp.exists(sPath):
    sftp.get_r(sPath, lPath)
print('done')
CodeGuru
  • 2,722
  • 6
  • 36
  • 52
  • Not answering the original question, but I may recommend to use Paramiko library (https://www.paramiko.org/) for SFTP (docs.paramiko.org/en/stable/api/sftp.html). I had very positive experience with it in the past. – Tim May 14 '20 at 13:26
  • 1
    Recursive download is not directly available in paramiko I feel. – CodeGuru May 14 '20 at 13:31
  • 1
    Thanks @Martin it helped alot, you made my day. – CodeGuru May 14 '20 at 13:45

0 Answers0