in addition to type constraintsfile content can matter in text-mode output files require str instead of bytes for contentso there is no way in to write truly binary data to text-mode file depending on the encoding rulesbytes outside the default character set can sometimes be embedded in normal stringand they can always be written in binary mode (some of the following raise errors when displaying their string results in pythons prior to but the file operations work successfully)can' read truly binary data in text mode chr( xff'ychr( xfe'\xfeff is valid charfe is not an error in some pythons open('temp'' 'write( '\xff\xfe\xfd'typeerrormust be strnot bytes can' use arbitrary bytesopen('temp'' 'write('\xff\xfe\xfd' open('temp''wb'write( '\xff\xfe\xfd' can write if embeddable in str open('temp''rb'read( '\xff\xfe\xfdcan always read as binary bytes open('temp'' 'read(' \xfe\xfdcan' read text unless decodablean error in some pythons can also write in binary mode in generalhoweverbecause text-mode input files in must be able to decode content per unicode encodingthere is no way to read truly binary data in text modeas the next section explains using unicode files so farwe've been reading and writing basic text and binary files it turns out to be easy to read and write unicode text stored in files toobecause the open call accepts an encoding for text filesand arranges to run the required encoding and decoding for us automatically as data is transferred this allows us to process variety of unicode text created with different encodings than the default for the platformand store the same text in different encodings for different purposes reading and writing unicode in in factwe can effectively convert string to different encoded forms both manually with method calls as we did earlierand automatically on file input and output we'll use the following unicode string in this section to demonstratec:\codec:\python \python ' \xc \xe cs 'aabecfive-character decoded stringnon-ascii using unicode files