python - 使用 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")
解决方案
推荐阅读
- node.js - 在 nginx 高山映像上安装节点 15
- javascript - 如何将js函数变量传递给ajax
- reactjs - 为什么我的号码在 React 中似乎是一个对象?
- excel - 如何使用excel中结构化引用表的特定连续列在vba中设置范围?
- javascript - Google Cloud Speech-to-Text 无法在某些 iDevice 上正确转录流式音频
- list - 如何从列中提取值并将其作为浮动在 pyspark 中?
- r - 求和 R 中绘图的所有值
- php - 在 VS Code 中安装 Laravel 后出现 PHP“未定义”错误
- java - 出现错误 [taskdef] 无法从资源 org/apache/maven/resolver/ant/antlib.xml 加载定义
- node.js - nodemon没有在docker上重新启动