Created
December 10, 2013 04:53
-
-
Save bobwei/7885891 to your computer and use it in GitHub Desktop.
python script to remove BOM in java files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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