Python与远程计算机之间传输文件

时间:2013-11-22 07:44:43

标签: python-2.7 paramiko

我想将本地文件传输到服务器计算机。

import os

import paramiko

local_path = "/home/e100075/python/ss.txt"

remote_path = "/home/developers/screenshots/"

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('hostname', username="username", password="password")

ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls /tmp')

print "output", ssh_stdout.read() 

#Reading output of the executed command

error = ssh_stderr.read()

#Reading the error stream of the executed command

print "err", error, len(error)

#Transfering files to and from the remote machine

sftp = ssh.open_sftp()

sftp.get(remote_path, local_path)

sftp.put(local_path, remote_path)

sftp.close()

ssh.close()

运行程序后。我收到了以下错误

Traceback (most recent call last):

  File "file_copy.py", line 21, in <module>

    sftp.get(remote_path, local_path)

  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 606, in get

    fr = self.file(remotepath, 'rb')

  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 245, in open

    t, msg = self._request(CMD_OPEN, filename, imode, attrblock)

  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 635, in _request

    return self._read_response(num)

  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 682, in 
_read_response

    self._convert_status(msg)

  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 708, in 
_convert_status

    raise IOError(errno.ENOENT, text)

IOError: [Errno 2] No such file

如果你知道答案。请告诉我。

感谢阅读。

2 个答案:

答案 0 :(得分:0)

如果您想将文件放入服务器,则可能需要

sftp.put(local_path, remote_path)

不是

sftp.get(remote_path, local_path)

答案 1 :(得分:0)

您还需要提供文件名,否则会抛出错误

sftp.put( 'FILENAME.TXT', '/家庭/用户/性能/ destination.txt')

其中'filename.txt'必须在当前目录中,否则给出完整路径