python3: why writing to socket require bytes type while writing to a file require str ?

Chris Angelico rosuav at gmail.com
Fri Jul 22 05:09:33 EDT 2016


On Fri, Jul 22, 2016 at 6:52 PM, Yubin Ruan <ablacktshirt at gmail.com> wrote:
>     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 ?
>

Did you specify an encoding when you opened the file? Did you specify
an encoding when you opened the socket?

ChrisA



More information about the Python-list mailing list