python3: why writing to socket require bytes type while writing to a file require str ?
Frank Millman
frank at chagford.com
Fri Jul 22 05:21:47 EDT 2016
"Yubin Ruan" wrote in message
news:47f3acf9-8da2-4aad-a6f0-7a9efbdfe4c3 at googlegroups.com...
> In my understanding, writing to a file would requires that everything be
> written byte by byte. So writing objects of type 'bytes' to socket makes
> sense. But, how could python write to file using unicode(type 'str')? Does
> python encode 'str'(Unicode) automatically before writing things to file?
> If it's true, then why can't it do that automatic encoding when I trying
> to write a 'str' to socket ?
>
If you open a file for writing in text mode, using 'w', it expects a string.
If you open it in binary mode, using 'wb', it expects bytes.
HTH
Frank Millman
More information about the Python-list
mailing list