Killbom

killbom removes all UTF-8 BOMs from the specified files.

Notice that it literally removes every byte sequence of "\xef\xbb\xbf" which makes this tool unsuitable for binary files.

Last modified
Lines 63

Parent directory Download CGIread sitemap Main page

Quick links: killBOM main usage

  1. #!/usr/bin/python
  2. import sys
  3. '''
  4. Killbom removes all UTF-8 byte order marks from the specified files.
  5. It removes all sequences of '%EF%BB%BF'.
  6. **Unsuitable for binary files.**
  7. '''
  8. # Set this to whatever you want to name this script.
  9. # (Command to enter at shell.)
  10. script_name = 'killbom'
  11. def killBOM(filename):
  12.     '''
  13.     Remove UTF-8 BOM from the file `filename`.
  14.     '''
  15.     
  16.     try:
  17.         text = open(filename, 'rb').read()
  18.         
  19.         outfile = open(filename, 'wb')
  20.         outfile.write(text.replace(b'\xEF\xBB\xBF', b''))
  21.         outfile.close()
  22.     except IOError as error:
  23.         sys.stderr.write("IO error '{}': {}\n".format(
  24.             filename.replace("'","\\'"),
  25.             error
  26.         ))
  27. def usage():
  28.     '''
  29.     Print usage message to stderr.
  30.     '''
  31.     
  32.     sys.stderr.write('''Usage: {0} [file] ...
  33. {0} removes all UTF-8 BOM marks from files.
  34. WARNING: It really removes all sequences of '\\0xEF\\0xBB\\0xBF'.
  35. WARNING: It accepts no options. All arguments are treated as filenames.
  36. '''.format(script_name))
  37. def main():
  38.     '''
  39.     Call `usage` and call `killBOM` for all specified arguments.
  40.     '''
  41.     
  42.     if len(sys.argv) > 1:
  43.         for filename in sys.argv[1:]:
  44.             killBOM(filename)
  45.     else:
  46.         usage()
  47. if __name__ == '__main__':
  48.     main()