使用python通过sftp上传文件 [英] Upload file via sftp with python

查看:70
本文介绍了使用python通过sftp上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的代码,用python将文件上传到sftp服务器.我正在使用python 2.7

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?

我已启用日志记录.我发现该文件已上传到根文件夹而不是公共文件夹下.似乎srv.cd('public')无法正常工作.

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.

推荐答案

我找到了自己问题的答案.

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()

srv.put放入srv.cd

这篇关于使用python通过sftp上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆