python sftp下载文件到本地,python - 如何将远程文件复制/下载到本地(sftp)? - 堆栈内存溢出...

尝试将远程文件/目录复制到本地计算机,但出现错误

Traceback (most recent call last):

File "test_ssh.py", line 23, in

sftp_client.put(str(os.path.join(remote_dir,file)),os.path.join(local_dir, file))

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/sftp_client.py", line 757, in put

file_size = os.stat(localpath).st_size

OSError: [Errno 2] No such file or directory:

不确定,这是怎么了。 以下是片段

import paramiko

import os

import stat

remote_host = "10.10.10.10"

username = "user"

key_filename = "/path/to/key_filename"

port = 22

remote_path = "/remote/path/location"

latest_folder_name = "abc_123456"

local_dir = os.path.join(os.getcwd(),latest_folder_name)

remote_dir = os.path.join(remote_path, latest_folder_name)

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(remote_host, username=username, key_filename=key_filename, port=port)

sftp = ssh.open_sftp()

for file in sftp.listdir(remote_dir):

if stat.S_ISREG(sftp.stat(os.path.join(remote_dir, file)).st_mode):

print "FILE '{}'".format(file)

sftp.put(os.path.join(remote_dir,file),os.path.join(local_dir, file))

else:

print "DIRECTORY {}".format(file)

sftp.close()

这有什么问题吗?

尝试使用sftp.get(remotefile,localfile)下载时,出现错误

Traceback (most recent call last):

File "test_ssh.py", line 23, in

sftp.get(os.path.join(remote_dir,file),os.path.join(local_dir, file))

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/sftp_client.py", line 801, in get

with open(localpath, "wb") as fl:

IOError: [Errno 2] No such file or directory:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值