the same patterns generally hold true for output when writing unicode file in python codewe need more explicit encoding name to force the bom in utf- --"utf- does not write (or skipthe bombut "utf- -sigdoesopen('temp txt'' 'encoding='utf- 'write('spam\nspam\ ' open('temp txt''rb'read(no bom 'spam\ \nspam\ \nopen('temp txt'' 'encoding='utf- -sig'write('spam\nspam\ ' open('temp txt''rb'read(wrote bom '\xef\xbb\xbfspam\ \nspam\ \nopen('temp txt'' 'read(' >>?spam\nspam\nopen('temp txt'' 'encoding='utf- 'read('\ufeffspam\nspam\nopen('temp txt'' 'encoding='utf- -sig'read('spam\nspam\nkeeps bom skips bom notice that although "utf- does not drop the bomdata without bom can be read with both "utf- and "utf- -sig"--use the latter for input if you're not sure whether bom is present in file (and don' read this paragraph out loud in an airport security line!)open('temp txt'' 'write('spam\nspam\ ' open('temp txt''rb'read( 'spam\ \nspam\ \nopen('temp txt'' 'read('spam\nspam\nopen('temp txt'' 'encoding='utf- 'read('spam\nspam\nopen('temp txt'' 'encoding='utf- -sig'read('spam\nspam\ndata without bom either utf- works finallyfor the encoding name "utf- ,the bom is handled automaticallyon outputdata is written in the platform' native endiannessand the bom is always writtenon inputdata is decoded per the bomand the bom is always stripped because it' standard in this schemesys byteorder 'littleopen('temp txt'' 'encoding='utf- 'write('spam\nspam\ ' open('temp txt''rb'read( '\xff\xfes\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ open('temp txt'' 'encoding='utf- 'read('spam\nspam\nusing unicode files