注册 登录
编程论坛 Python论坛

实在不会了,救救孩子吧

python09 发布于 2021-11-12 10:15, 1043 次点击
有大佬用过python的paramiko模块么,我在一个windows上装了freeSSHd,然后用paramiko模拟shh连接进行下载文件,但是有的中文名的文件能成功下载,有的报错,查找了许多资料都没解决,排查估计是sftp.listdir模块不能识别,但是我找不到能解决问题的办法,求求大佬们了,能指点指点孩子么
报错信息:UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 66-67: invalid continuation byte
这里是代码:
程序代码:
import paramiko
import os


def DownLoadFile(sftp, LocalFile, RemoteFile):  # 下载当个文件
    file_handler = open(LocalFile, 'wb')
    print(file_handler)
    sftp.get(RemoteFile, LocalFile)  # 下载目录中文件
    file_handler.close()
    return True


def DownLoadFileTree(sftp, LocalDir, RemoteDir):  # 下载整个目录下的文件
    if not os.path.exists(LocalDir):
        os.makedirs(LocalDir)
    for file in sftp.listdir(RemoteDir):
        Local = os.path.join(LocalDir, file)
        Remote = os.path.join(RemoteDir, file)
        if file.find(".") == -1:  # 判断是否是文件
            if not os.path.exists(Local):
                os.makedirs(Local)
            DownLoadFileTree(sftp, Local, Remote)
        else:  # 文件
            print(file)
            DownLoadFile(sftp, Local, Remote)
    return "complete"


if __name__ == '__main__':
    host = '192.168.0.190'  # 主机
    port = 22 # 端口
    username = 'YL123'  # 用户名
    password = '123456'  # 密码
    sf = paramiko.Transport((host, port))
    sf.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(sf)
    local = 'e:\sshtest'  # 本地文件
    remote = '/freeSSHd/P211027178Y/'  # 远程文件或目录
    DownLoadFileTree(sftp, local, remote)

0 回复
1