Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file via sftp with python

I wrote a simple code to upload a file to a sftp server in python. I am using python 2.7

import pysftp

srv = pysftp.Connection(host="www.destination.com", username="root",
password="password",log="./temp/pysftp.log")

srv.cd('public') #chdir to public
srv.put('C:\Users\XXX\Dropbox\test.txt') #upload file to nodejs/

# Closes the connection
srv.close()

The file did not appear on the server. However, no error message appeared. What is wrong with the code?

EDIT: 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.

like image 244
guagay_wk Avatar asked Nov 17 '15 08:11

guagay_wk


1 Answers

I found the answer to my own question.

import pysftp

srv = pysftp.Connection(host="www.destination.com", username="root",
password="password",log="./temp/pysftp.log")

with srv.cd('public'): #chdir to public
    srv.put('C:\Users\XXX\Dropbox\test.txt') #upload file to nodejs/

# Closes the connection
srv.close()

Put the srv.put inside with srv.cd

like image 190
guagay_wk Avatar answered Sep 20 '22 23:09

guagay_wk