我正在使用Pycharm和Python 3.4开发Django 1.8.4项目
我试图在shell上创建一个Model对象来测试一些东西并在执行这一行时:
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")))
我收到此错误:
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
有人能告诉我命令的编码问题在哪里吗?
谢谢!
答案 0 :(得分:1)
使用open
这样的open('C:\\...', encoding='utf-8')
函数意味着,使用utf-8编码读取我的文本文件。由于你试图读取一个mp3文件(二进制),这不起作用。
阅读方法是:open('C:\\coolSong.mp3', mode='rb')
这意味着,请阅读我的旁边文件。
在此处查看关于the open function and it's modes和overview about io的真正好的解释。