title: Convert UTF-8 with BOM to UTF-8 with no BOM in Python
body: <p>Two questions here. I have a set of files which are usually UTF-8 with BOM. I'd like to convert them (ideally in place) to UTF-8 with no BOM. It seems like <code>codecs.StreamRecoder(stream, encode, decode, Reader, Writer, errors)</code> would handle this. But I don't really see any good examples on usage. Would this be the best way to handle this?  </p>
  
  <pre><code>source files:
  Tue Jan 17$ file brh-m-157.json 
  brh-m-157.json: UTF-8 Unicode (with BOM) text
  </code></pre>
  
  <p>Also, it would be ideal if we could handle different input encoding wihtout explicitly knowing (seen ASCII and UTF-16). It seems like this should all be feasible. Is there a solution that can take any known Python encoding and output as UTF-8 without BOM?</p>
  
  <p><strong>edit 1</strong>  proposed sol'n from below (thanks!)</p>
  
  <pre><code>fp = open('brh-m-157.json','rw')
  s = fp.read()
  u = s.decode('utf-8-sig')
  s = u.encode('utf-8')
  print fp.encoding  
  fp.write(s)
  </code></pre>
  
  <p>This gives me the following error:  </p>
  
  <pre><code>IOError: [Errno 9] Bad file descriptor
  </code></pre>
  
  <h3>Newsflash</h3>
  
  <p>I'm being told in comments that the mistake is I open the file with mode 'rw' instead of 'r+'/'r+b', so I should eventually re-edit my question and remove the solved part.</p>