首页 > 解决方案 > 使用 pysftp 从服务器读取文件失败

问题描述

我正在尝试使用 python 从 sftp 服务器读取最新文件,但它显示错误消息为 OSError:错误消息。有人可以帮我提前谢谢。

import os
from six import BytesIO
import base64
myUsername = "a123"
myPassword = "124qwe"
myHostname = "abc.com"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=myHostname, username=myUsername,password=myPassword,cnopts=cnopts) as sftp:
    print ("Connection succesfully stablished ... ")
    remoteFilePath = r"\GLOBAL\test\c1a0604437be43598a0dfe7ddef5b992_1"
    #path = os.path.join(remoteFilePath,folder_name,tray_number,file_name +'.pdf')
    sftp.chdir(remoteFilePath)
    filename=[]
    for f in sorted(sftp.listdir_attr(), key=lambda k: k.st_mtime, reverse=True):
        filename.append(f.filename)
    path = os.path.join(remoteFilePath,filename[0])
    try:
        print(sftp.stat(path)) #Throws an filenot error if the doesnt exists
        flo = BytesIO()
        sftp.getfo(path, flo)
        flo.seek(0)
        encoded_img_data = base64.b64encode(flo.getvalue())
        img_data=encoded_img_data.decode('utf-8')
        print("done")
    except FileNotFoundError:
        sftp.close()
        print("not done")

标签: pythonparamikopysftp

解决方案


推荐阅读