简体   繁体   English

使用 Python (pysftp) 将文件上传到 SFTP 失败并显示“没有此类文件”

[英]File upload to SFTP using Python (pysftp) fails with "No such file"

I am trying to upload a CSV file to an SFTP server from my machine.我正在尝试将 CSV 文件从我的机器上传到 SFTP 服务器。 I don't know why my code is not able to find the file.我不知道为什么我的代码无法找到该文件。 Need little help to identify the issue.几乎不需要帮助来确定问题。 Here's my code这是我的代码

import pysftp as sftp
def sftpExample():
    try:
        cnopts = sftp.CnOpts()
        cnopts.hostkeys = None
        s = sftp.Connection(host='abc.net', username='xyz', password='aaaaaaaaaaaa',cnopts=cnopts)

        remotepath = 'http://sftp.abc.net/uploads/'
        localpath = '/Users/ashish.verma/Desktop/Text.rtf'
        s.put(localpath,remotepath)

        s.close()

    except Exception as e:
        print(e)
sftpExample()

Connection to the SFTP server is successful, but I don't know why my code is not able to find the file on my local machine.连接到 SFTP 服务器成功,但我不知道为什么我的代码无法在我的本地机器上找到该文件。 Error message says:错误信息说:

No such file没有这样的文件

The remotepath argument of pysftp Connection.put method is a file path . pysftp Connection.put方法remotepath参数是一个文件路径 Not directory URL , let only HTTP URL.不是目录 URL ,只让HTTP URL。

It should be like:它应该是这样的:

remotepath = '/uploads/Text.rtf'
s.put(localpath, remotepath)

Alternatively you can omit the argument, making pysftp upload the file to the current remote working directory under the original file name (taken from localpath ):或者,您可以省略该参数,使 pysftp 以原始文件名(取自localpath )将文件上传到当前远程工作目录:

s.cd('/uploads')
s.put(localpath)

Obligatory warning: Do not set cnopts.hostkeys = None , unless you do not care about security.强制性警告:不要设置cnopts.hostkeys = None ,除非您不关心安全性。 For the correct solution see Verify host key with pysftp .有关正确的解决方案,请参阅使用 pysftp 验证主机密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM