FileNotFoundError:[错误2]没有这样的文件或目录:'/ home / ubicomp / ubicomp / questions_check'。 (将文件放入SFTP服务器。)

时间:2018-10-17 14:40:22

标签: python django ubuntu-16.04 pysftp

我正在尝试使用以下代码将文件放入sftp服务器中。我正在使用Ubuntu 16.04。

import pysftp as sftp

def sftp_file_transfer():
    s = sftp.Connection('ip_address', username='username', password='password')
    remote_path = '/var/sftp/uploads/'
    local_path = '/home/ubicomp/ubicomp/questions_check'
    s.put(local_path, remote_path)
    s.close()

我改变了存在的道路。但是我得到了错误。而且我找不到问题。

Traceback (most recent call last):
File "/home/ubicomp/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/ubicomp/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/ubicomp/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ubicomp/ubicomp/import_files/views.py", line 34, in sftp_file_transfer
s.put(local_path, remote_path)
File "/home/ubicomp/lib/python3.5/site-packages/pysftp/__init__.py", line 364, in put
confirm=confirm)
File "/home/ubicomp/lib/python3.5/site-packages/paramiko/sftp_client.py", line 757, in put
file_size = os.stat(localpath).st_size
FileNotFoundError: [Errno 2] No such file or directory: '/home/ubicomp/ubicomp/questions_check'

1 个答案:

答案 0 :(得分:0)

我已经解决了问题。远程路径必须更改。而且效果很好。

import pysftp as sftp

def sftp_file_transfer():
s = sftp.Connection('ip_address', username='username', password='password')
remote_path = '/uploads/questions_check.csv'
local_path = '/home/ubicomp/ubicomp/questions_check.csv'
s.put(local_path, remote_path)
s.close()

我不得不在远程路径中提到一个具有相同扩展名的文件名,然后取消/ var / sftp部分