Skip to content

Instantly share code, notes, and snippets.

@ZiTAL ZiTAL/bom.py
Last active Aug 29, 2015

Embed
What would you like to do?
Python: Remove BOM from UTF-8 files
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os, re, binascii
if(len(sys.argv)<2):
print 'Errorea: Karpeta argumentu bidez sartu'
sys.exit()
else:
dir = sys.argv[1]
# fitxategien zerrenda
file_list = []
def scandir(dir):
if os.path.isdir(dir):
dir_list = os.listdir(dir)
for f in dir_list:
path = os.path.join(dir, f)
if os.path.isfile(path):
file_list.append(path)
elif os.path.isdir(path):
scandir(path)
# fitxategien zerrenda hartu
scandir(dir)
print "Fitxategi honeetatik BOM-a kentzen:"
for f in file_list:
# 1go lerroa bakarrik irakurri eta hexadecimal formatura pasa
hex = binascii.hexlify(open(f, 'r').readline())
# bom-a badauka ikusi
if(re.match("^efbbbf", hex)):
print f
file = open(f, 'r')
# fitxategi guztia hexadecimal formatura pasa
hex = binascii.hexlify(file.read())
# bom-a kendu
hex = re.sub("^efbbbf", '', hex)
# hex -> testura
deco = hex.decode("hex")
file = open(f, 'w')
file.write(deco)
file.close()
print "Eginda"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.