Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

file

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?

like image 519
Miguel Santos Avatar asked Jun 11 '18 11:06

Miguel Santos


1 Answers

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())
like image 200
Serge Ballesta Avatar answered Sep 22 '22 13:09

Serge Ballesta