问题
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