Python pysftp.put会引发"No such file"(没有此类文件)例外,尽管文件已上传 [英] Python pysftp.put raises "No such file" exception although file is uploaded

查看:708
本文介绍了Python pysftp.put会引发"No such file"(没有此类文件)例外,尽管文件已上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pysftp连接到服务器并将文件上传到该服务器.

I am using pysftp to connect to a server and upload a file to it.

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
self.sftp = pysftp.Connection(host=self.serverConnectionAuth['host'], port=self.serverConnectionAuth['port'],
                              username=self.serverConnectionAuth['username'], password=self.serverConnectionAuth['password'], 
                              cnopts=cnopts)
self.sftp.put(localpath=self.filepath+filename, remotepath=filename)

有时它可以正常运行,没有错误,但是有时它会正确放置文件,但是会引发以下异常.该文件由服务器上运行的另一个程序读取和处理,因此我可以看到该文件在那里并且没有损坏

Sometimes it does okay with no error, but sometime it puts the file correctly, BUT raises the following exception. The file is read and processed by another program running on the server, so I can see that the file is there and it is not corrupted

  File "E:\Anaconda\envs\py35\lib\site-packages\pysftp\__init__.py", line 364, in put
    confirm=confirm)
  File "E:\Anaconda\envs\py35\lib\site-packages\paramiko\sftp_client.py", line 727, in put
    return self.putfo(fl, remotepath, file_size, callback, confirm)
  File "E:\Anaconda\envs\py35\lib\site-packages\paramiko\sftp_client.py", line 689, in putfo
    s = self.stat(remotepath)
  File "E:\Anaconda\envs\py35\lib\site-packages\paramiko\sftp_client.py", line 460, in stat
    t, msg = self._request(CMD_STAT, path)
  File "E:\Anaconda\envs\py35\lib\site-packages\paramiko\sftp_client.py", line 780, in _request
    return self._read_response(num)
  File "E:\Anaconda\envs\py35\lib\site-packages\paramiko\sftp_client.py", line 832, in _read_response
    self._convert_status(msg)
  File "E:\Anaconda\envs\py35\lib\site-packages\paramiko\sftp_client.py", line 861, in _convert_status
    raise IOError(errno.ENOENT, text)
FileNotFoundError: [Errno 2] No such file

如何防止该异常?

推荐答案

根据您的评论,我认为文件在某些​​服务器端进程上传后不久就被删除了.

From your comment, I assume that the file is removed very shortly after it is uploaded by some server-side process.

默认情况下, pysftp.Connection.put 会验证通过检查目标文件的大小来上传.如果服务器端进程设法过快地删除文件,则读取文件大小将失败.

By default pysftp.Connection.put verifies the upload by checking a size of the target file. If the server-side processes manages to remove the file too fast, reading the file size would fail.

您可以通过将confirm参数设置为False来禁用上传后检查:

You can disable the post-upload check by setting confirm parameter to False:

self.sftp.put(localpath=self.filepath+filename, remotepath=filename, confirm=False)

我相信支票还是多余的,请参阅
如何在SFTP文件传输过程中执行校验和以确保数据完整性?

I believe the check is redundant anyway, see
How to perform checksums during a SFTP file transfer for data integrity?

有关Paramiko(pysftp在内部使用)的类似问题,请参见:
Paramiko put方法抛出"[[Errno 2]文件未找到"". FTP服务器是否触发了上传后自动移动文件的功能

For a similar question about Paramiko (which pysftp uses internally), see:
Paramiko put method throws "[Errno 2] File not found" if FTP server has trigger to automatically move file upon upload

这篇关于Python pysftp.put会引发"No such file"(没有此类文件)例外,尽管文件已上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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