nowthe same rules apply to text filesbecause unicode strings are stored in files as encoded bytes when writingwe can encode in any format that accommodates the string' characters when readingthoughwe generally must know what that encoding is or provide one that formats characters the same wayopen('ldata'' 'encoding='latin- 'write( open('udata'' 'encoding='utf- 'write( open('ldata'' 'encoding='latin- 'read('aabacopen('udata'' 'encoding='utf- 'read('aabacstore in latin- format store in utf- format ok if correct name given open('ldata'' 'read(elsemay not work 'aabacopen('udata'' 'read(unicodeencodeerror'charmapcodec can' encode characters in position - cha open('ldata'' 'encoding='utf- 'read(unicodedecodeerror'utf codec can' decode bytes in position - invalid dat open('udata'' 'encoding='latin- 'read(unicodeencodeerror'charmapcodec can' encode character '\xc in position by contrastbinary mode files don' attempt to decode into unicode stringthey happily read whatever is presentwhether the data was written to the file in text mode with automatically encoded str strings (as in the preceding interactionor in binary mode with manually encoded bytes stringsopen('ldata''rb'read( ' \xc \xe copen('udata''rb'read( ' \xc \ \xc \xa copen('sdata''wb'writes encode('utf- 'open('sdata''rb'read( '\xff\xfea\ \xc \ \ \xe \ \ return value unicode and the text widget the application of all this to tkinter text displays is straightforwardif we open in binary mode to read byteswe don' need to be concerned about encodings in our own code-tkinter interprets the data as expectedat least for these two encodingsfrom tkinter import text text( insert(' 'open('ldata''rb'read() pack( get(' ''end''aabac\nstring appears in gui ok text