string argument expected, got 'bytes' in buffer.write

吃可爱长大的小学妹 提交于 2021-02-08 13:08:33

问题


I have this:

from io import StringIO
buffer = StringIO()

latest_file = 'C:\\Users\\miguel.santos\\Desktop\\meo_snapshots\\Snapshot_14.jpg'

buffer.write(open(latest_file,'rb').read())

TypeError: string argument expected, got 'bytes'

Any ideas on how to solve?


回答1:


io.StringIO is for unicode text, its counterpart for bytes is io.BytesIO. As your undelying file is a binary jpg, you really should use the latter:

from io import BytesIO
buffer = BytesIO()

latest_file = 'C:\\Users\\miguel.santos\\Desktop\\meo_snapshots\\Snapshot_14.jpg'

buffer.write(open(latest_file,'rb').read())


来源:https://stackoverflow.com/questions/50797043/string-argument-expected-got-bytes-in-buffer-write

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!