title: Python Writing a UCS-2 Little Endian (utf-16-le) file with BOM
body: <p>I have a 3rd party app that reads in a Unicode file of utf-16-le. For some reason it has to have a BOM or it won’t read the file. </p>
  
  <p>I had a lot of difficulty writing an output file of this encoding so I thought I’ll document my solution here for others who might come across the same issue. </p>
  
  <pre><code>import codecs
  
  mytext = "Help me"
  
  with open("c:\\temp\\myFile.txt", 'w') as f:
      f.write(codecs.BOM_UTF16_LE)
      f.write(mytext.encode('utf-16-le'))
  </code></pre>
  
  <p>I was seriously misled by the chosen <a href="https://stackoverflow.com/a/5202815/510937">‘correct answer’</a> in the following thread.  </p>
  
  <pre><code>write('\ufeff')
  </code></pre>
  
  <p>Does not write the required BOM marker into the file (at least it didn’t for me using Python 2.7) 
  You need </p>
  
  <pre><code>write('\xff\xfe')
  </code></pre>
  
  <p>I don’t have enough reputation to post that so I’m posting my own 
  <a href="https://stackoverflow.com/questions/5202648/adding-bom-unicode-signature-while-saving-file-in-python">Adding BOM (unicode signature) while saving file in python</a></p>
  
  <p>I think this is the exact same issue in Java 
  <a href="https://stackoverflow.com/questions/17081953/write-text-file-in-ucs-2-little-endian-java">Write text file in UCS-2 Little Endian, Java</a></p>