import sys with open('config.toml', 'rb') as f: data = f.read() BOM = bytes([0xef, 0xbb, 0xbf]) if data.startswith(BOM): with open('config.toml', 'wb') as f: f.write(data[3:]) print('BOM removed') else: print('No BOM found') sys.exit(0)