How to upload all files from Windows folder to SFTP folder in Python

Question:

I am trying to upload all the files in my Windows folder to SFTP folder. Below is my code. If there are no files in csv folder, nothing should happen. How to achieve this?

with pysftp.Connection(host, username, password) as sftp:
    localpath = r'filescsv'
    remotepath = '/2021/November'
    sftp.put(localpath,remotepath)

It throws the below error for now.

PermissionError: [Errno 13] Permission denied: ‘filescsv’

Asked By: massu1000

||

Answers:

The Connection.put can upload a single file only.

To upload all files in a folder, you can use Connection.put_d. For a recursive upload, you can use Connection.put_r. Unfortunately neither works on Windows.

But it’s easy to implement a portable recursive upload, see:
Python pysftp put_r does not work on Windows


Note that pysftp is dead project. Consider switching to Paramiko. See pysftp vs. Paramiko.

Answered By: Martin Prikryl
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.