Pablo Estrada
Pablo Estrada

Reputation: 3452

UnicodeDecodeError on Pycharm Shell

I´m working on a Django 1.8.4 project with Pycharm and Python 3.4

I was trying to create a Model Object on the shell to test some stuff and when executing this line:

song1 = Cancion.objects.create(nombre="City Lights",
                               audio_file=File(open("C:\\Users\\pablo\\Desktop\\Music\\Brian Culbertson  - Another Long Night Out (2014)\\01 - City Lights (Feat. Lee Ritenour).mp3",encoding="utf-8")))

I get this error:

Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 348, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 734, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 762, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 846, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 885, in _do_insert
    using=using, raw=raw)
  File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 920, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 973, in execute_sql
    for sql, params in self.as_sql():
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 931, in as_sql
    for obj in self.query.objs
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 931, in <listcomp>
    for obj in self.query.objs
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 929, in <listcomp>
    ) for f in fields
  File "C:\Python34\lib\site-packages\django\db\models\fields\files.py", line 315, in pre_save
    file.save(file.name, file, save=False)
  File "C:\Python34\lib\site-packages\django\db\models\fields\files.py", line 94, in save
    self.name = self.storage.save(name, content, max_length=self.field.max_length)
  File "C:\Python34\lib\site-packages\django\core\files\storage.py", line 64, in save
    name = self._save(name, content)
  File "C:\Python34\lib\site-packages\django\core\files\storage.py", line 253, in _save
    for chunk in content.chunks():
  File "C:\Python34\lib\site-packages\django\core\files\base.py", line 85, in chunks
    data = self.read(chunk_size)
  File "C:\Python34\lib\codecs.py", line 313, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 21: invalid start byte

Can anybody tell me where´s the encoding problem of my command?

Thank you!

Upvotes: 2

Views: 1404

Answers (1)

danielcorreia
danielcorreia

Reputation: 2136

Using open function like this open('C:\\...', encoding='utf-8') means, read my text file with utf-8 encoding. Since you trying to read an mp3 file (binary) this doesn't work.

The way to read it is: open('C:\\coolSong.mp3', mode='rb') which means, read my bynary file.

Checkout really good explanations here about the open function and it's modes and the overview about io.

Upvotes: 1

Related Questions