Skip to content

Instantly share code, notes, and snippets.

@bobwei
Created December 10, 2013 04:53
Show Gist options
  • Save bobwei/7885891 to your computer and use it in GitHub Desktop.
Save bobwei/7885891 to your computer and use it in GitHub Desktop.
python script to remove BOM in java files
import os, sys, codecs
for dirpath, dirnames, filenames in os.walk('./mosorder'):
# print dirpath, dirnames, filenames
print 'dirpath = {}'.format(dirpath)
for filename in filenames:
if not filename.find('.java') > 0:
continue
# print 'filename = {}'.format(filename)
path = '{}/{}'.format(dirpath, filename)
print 'path = {}'.format(path)
BUFSIZE = 4096
BOMLEN = len(codecs.BOM_UTF8)
# path = sys.argv[1]
with open(path, "r+b") as fp:
chunk = fp.read(BUFSIZE)
if chunk.startswith(codecs.BOM_UTF8):
i = 0
chunk = chunk[BOMLEN:]
while chunk:
fp.seek(i)
fp.write(chunk)
i += len(chunk)
fp.seek(BOMLEN, os.SEEK_CUR)
chunk = fp.read(BUFSIZE)
fp.seek(-BOMLEN, os.SEEK_CUR)
fp.truncate()
fp.seek(0)
fp.write(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment