python - TypeError: expected str, bytes or os.PathLike object, not FileStorage 不断弹出
问题描述
def convertToBinaryData(filename):
# Convert digital data to binary format
with open(filename, 'rb') as file:
binaryData = file.read()
return binaryData
这是我将图像转换为二进制的功能...
uploaded_file = request.files['file']
if uploaded_file.filename != '':
uploaded_file.save(uploaded_file.filename)
empPicture = convertToBinaryData(uploaded_file)
这是接收和保存上传文件的代码块,但是,当它运行时,我收到此错误...
with open(filename, 'rb') as file:
TypeError: expected str, bytes or os.PathLike object, not FileStorage
我对python很陌生,我已经坚持了一段时间,任何帮助将不胜感激。提前致谢
解决方案
在调用“ convertToBinaryData ”时,您传递的是“ upload_file ”,它不是文件名而是对象。
您需要将文件名(如果保存在自定义位置,则使用正确的路径)传递给您的“ convertToBinaryData ”功能。
像这样的东西:
convertToBinaryData(uploaded_file.filename)
推荐阅读
- unity3d - 命名空间“UnityEngine”中不存在 Unity 类型或命名空间名称“InputSystem”
- rpa - 从pdf中选择文本并将其插入excel
- r - 如何根据客户 ID、日期、销售额删除在销售列中具有相同值但符号不同的行
- javascript - 承诺全部进行四个 api 调用而不是两个 api 调用
- c - 加密更新时出现 OpenSSL AES 分段错误
- linux - XSLT:自动行尾调整开关。或者如何在 Windows 上生成单个 LF 字符
- tensorflow - 用于道路分割的 Deeplab
- sql - 使用SQL判断in date,即将过期和过期
- python - SQLAlchemy 中的 ORM 模型会自动创建表吗?
- python - 有没有办法在 tkinter 中复制框架或条目?