Upload file via sftp with python

元气小坏坏 提交于 2019-11-29 08:18:17

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/33751854/upload-file-via-sftp-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!