Python Paramiko,权限错误: [Errno 13] Permission denied when get files from remote server

标签 python sftp paramiko

import paramiko, os
paramiko.util.log_to_file('E:\Automation\paramiko.log')
from stat import S_ISDIR
host = "xx.xx.xxx.xxx"
port = 22
transport = paramiko.Transport((host, port))
password = "password"
username = "username"
#transport.set_missing_host_key_policy(paramiko.AutoAddPolicy())
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)

def sftp_walk(remotepath):
    path=remotepath
    files=[]
    folders=[]
    for f in sftp.listdir_attr(remotepath):
        if S_ISDIR(f.st_mode):
            folders.append(f.filename)
        else:
            files.append(f.filename)
    if files:
        yield path, files
    for folder in folders:
        new_path=os.path.join(remotepath,folder)
        for x in sftp_walk(new_path):
            yield x


for path,files  in sftp_walk("." or '/SourceData/CSV.EXTRACT/'):
    for file in files:
        sftp.get(os.path.join(os.path.join(path,file)), 'E:\InsightImport\CSV_EXTRACT')
E:\Automation>python dw.export.py
Traceback (most recent call last):
  File "dw.export.py", line 33, in <module>
    sftp.get(os.path.join(os.path.join(path,file)), 'E:\InsightImport\CSV_EXTRAC
  File "C:\Users\svc-cbsbir\AppData\Local\Programs\Python\Python37\lib\site-pack
    with open(localpath, "wb") as fl:
PermissionError: [Errno 13] Permission denied: 'E:\\InsightImport\\CSV_EXTRACT'

最佳答案

SFTPClient.get 的第二个参数是本地文件的路径。虽然您似乎将路径传递给目录

此外,您不应在 SFTP 路径上使用 os.path.joinos.path.join 用于本地路径。 SFTP 始终使用正斜杠,而os.path.join 使用本地操作系统特定的分隔符(在 Windows 上反斜杠)。

sftp.get(path + '/' + file, os.path.join('E:\InsightImport\CSV_EXTRACT', file))

(或者您可以使用 PosixPath)

关于Python Paramiko,权限错误: [Errno 13] Permission denied when get files from remote server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53760297/

相关文章:

python - 以交互式 ssh 模式在进程内调用多个命令

python - IO错误 : [Errno 2] No such file - Paramiko put()

Python - 通过不在模块级别导入来优化?

python - 如果尚未使用 celery 安排任务,则允许执行任务

linux - 从 Linux 到 Windows SFTP 服务器的 Rsync

java - Apache Mina SSHD 1.0.0 设置用户目录和映射

python - 在 Centos 中使用 pip 安装加密 python 库时出错

python - 在 Ruby 中检查变量类型

python - 将 Django 与亚马逊的数据库集成 'SimpleDB'

c# - 使用 WinSCP .NET 程序集仅通过 SFTP 协议(protocol)删除或移动选定的文件