I wrote a simple code to upload a file to a SFTP server in Python. I am using Python 2.7.
JavaScript
x
11
11
1
import pysftp
2
3
srv = pysftp.Connection(host="www.destination.com", username="root",
4
password="password",log="./temp/pysftp.log")
5
6
srv.cd('public') #chdir to public
7
srv.put('C:UsersXXXDropboxtest.txt') #upload file to nodejs/
8
9
# Closes the connection
10
srv.close()
11
The file did not appear on the server. However, no error message appeared. What is wrong with the code?
I have enabled logging. I discovered that the file is uploaded to the root folder and not under public
folder. Seems like srv.cd('public')
did not work.
Advertisement
Answer
I found the answer to my own question.
JavaScript
1
11
11
1
import pysftp
2
3
srv = pysftp.Connection(host="www.destination.com", username="root",
4
password="password",log="./temp/pysftp.log")
5
6
with srv.cd('public'): #chdir to public
7
srv.put('C:UsersXXXDropboxtest.txt') #upload file to nodejs/
8
9
# Closes the connection
10
srv.close()
11
Put the srv.put
inside with srv.cd