Python Paramiko,PermissionError:[Errno 13]从远程服务器获取文件时,权限被拒绝

时间:2018-12-13 10:56:59

标签: 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'

1 个答案:

答案 0 :(得分:1)

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))
相关问题