Python Forum
open(file, 'rb') raises UnicodeDecodeError
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
open(file, 'rb') raises UnicodeDecodeError
#1
I'm creating a project with Django that involves user uploading images. To test for an image and how it's stored in Django's default storage, I'm using the combination of SimpleUploadedFile, io.BytesIO(), and the Pillow Image class. When I test for the successful creation of a Django model instance (Photo) a UnicodeDecodeError is raised.

I've gone over the Python docs and read content on other forums that dealt with similar problems creating a mock image for testing purposes. I have had zero luck resolving this. What can be done to resolve this so that open() reads the BytesIO file successfully?

Error:
ERROR: setUpClass (photos.tests.test_models.TestPhotoModel) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\..\..\..\..\..\venv\lib\site-packages\django\test\testcases.py", line 1137, in setUpClass cls.setUpTestData() File "C:\..\..\..\..\..\photos\tests\test_models.py", line 23, in setUpTestData content=open(file.read(), 'rb') UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
from io import BytesIO
from unittest.mock import Mock, patch

from django.test import TestCase
from django.db.models import ImageField
from django.core.files.uploadedfile import SimpleUploadedFile
from PIL import Image

from ..models import Photo

class TestPhotoModel(TestCase):

    @classmethod
    def setUpTestData(cls):
        file = BytesIO()
        image = Image.new("RGB", (50, 50), 'red')
        image.save(file, "png")
        file.seek(0)
        data = {
            'title': "Title",
            'image': SimpleUploadedFile(
                'test_image.png',
                content=open(file.read(), 'rb')
            )
        }
        cls.new_photo = Photo.objects.create(**data)

    def test_photo_instance_created(self):
        total_photos = Photo.objects.count()
        self.assertEqual(total_photos, 1)
Reply
#2
Perhaps you mean content=file.getvalue()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 312 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,085 Dec-14-2023, 08:03 AM
Last Post: shanoger
  How can i combine these two functions so i only open the file once? cubangt 4 851 Aug-14-2023, 05:04 PM
Last Post: snippsat
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 16: invalid cont Melcu54 3 4,910 Mar-26-2023, 12:12 PM
Last Post: Gribouillis
  I cannot able open a file in python ? ted 5 3,281 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,356 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 18,828 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,395 May-06-2022, 01:44 PM
Last Post: Larz60+
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,017 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Open an excel file Newbie1114 1 2,329 Jun-16-2021, 09:11 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020