Python Paramiko,PermissionError:[Errno 13] 从远程服务器获取文件时权限被拒绝 [英] Python Paramiko, PermissionError: [Errno 13] Permission denied when get files from remote server

查看:80
本文介绍了Python Paramiko,PermissionError:[Errno 13] 从远程服务器获取文件时权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 是本地文件的路径.虽然您似乎传递了一个目录的路径.

The second argument to SFTPClient.get is a path to a local file. While you seem to pass a path to a directory.

此外,您不应在 SFTP 路径上使用 os.path.join.os.path.join 用于 local 路径.SFTP 总是使用 forward 斜杠,而 os.path.join 使用本地操作系统特定的分隔符(Windows 上的 back 斜杠).

Also, you should not use os.path.join on SFTP paths. The os.path.join is for local paths. SFTP always uses forward slashes, while os.path.join uses local OS-specific separators (back slashes on Windows).

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

(或者你可以使用 PosixPath)

这篇关于Python Paramiko,PermissionError:[Errno 13] 从远程服务器获取文件时权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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