Difficulty writing .png file python

804 Views Asked by At

I'm trying to write a .png file that I retrieve from the web with Python.

with open(fileName, 'w+') as f: 
      f.write(data)

'ascii' codec can't encode character u'\ufffd' in position 0: ordinal not in range(128)

I tried to do data.encode('utf-8'), but when I use magic with Python to get the mime type, it detects it as a .bin file.

Is there any defined way to handle this? This happens some times and not others.

Here's some more information:

Github link to code

API reference

1

There are 1 best solutions below

2
On

You need to open the file in binary mode:

with open(fileName, 'wb') as f: 
    f.write(data)