binary mode for undecoded bytesbut drop \rc:\pp \gui\tourpython from tkinter import use bytesstrip \ if any text(data open('jack txt''rb'read(data data replace( '\ \ ' '\ ' insert(' 'datat pack( get(' ''end')[: ' all work and no play makes jack dull boy \ all work and no plato save content laterwe can either add the \ characters back on windows onlymanually encode to bytesand save in binary modeor we can open in text mode to make the file object restore the \ if needed and encode for usand write the str content string directly the second of these is probably simpleras we don' need to care about platform differences either waythoughwe still face an encoding step--we can either rely on the platform default encoding or obtain an encoding name from user interfaces in the followingfor examplethe text-mode file converts end-lines and encodes to bytes internally using the platform default if we care about supporting arbitrary unicode types or run on platform whose default does not accommodate characters displayedwe would need to pass in an explicit encoding argument (the python slice operation here has the same effect as fetching through tk' "end- cposition specification)continuing prior listing content get(' ''end')[:- open('copyjack txt'' 'write(content ^ drop added \ at end use platform default text mode adds \ on win :\pp \gui\tourfc jack txt copyjack txt comparing files jack txt and copyjack txt fcno differences encountered supporting unicode in pyedit (aheadwe'll see use case of accommodating the text widget' unicode behavior in the larger pyedit example of reallysupporting unicode just means supporting arbitrary unicode encodings in text files on opens and savesonce in memorytext processing can always be performed in terms of strsince that' how tkinter returns content to support unicodepyedit will open both input and output files in text mode with explicit encodings whenever possibleand fall back on opening input files in binary mode only as last resort this avoids relying on the limited unicode support tk provides for display of raw byte strings to make this policy workpyedit will accept encoding names from wide variety of sources and allow the user to configure which to attempt encodings may be obtained from user dialog inputsconfiguration file settingsthe platform defaultthe prior text