Wait... WHAT?

Ian Kelly ian.g.kelly at gmail.com
Thu Feb 13 01:01:13 CET 2014

On Wed, Feb 12, 2014 at 3:14 PM, Tim Chase
<python.list at tim.thechases.com> wrote:
> On 2014-02-12 14:35, Ian Kelly wrote:
>> You can't write lists directly to files.  You can only write strings
>> to files.  To write and read a list, you'll need to first serialize
>> it and later deserialize it.
> To be pedantic, you can only write *bytes* to files, so you need to
> serialize your lists (or other objects) to strings and then encode
> those to bytes; or skip the string and encode your list/object
> directly to bytes.

The file was opened in text mode, not binary mode.

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('test.txt', 'w')
>>> f.write(b'hello world')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not bytes

More information about the Python-list mailing list