Skip to content

Instantly share code, notes, and snippets.

@AlmightyOatmeal
Last active May 8, 2018 22:43
Show Gist options
  • Save AlmightyOatmeal/372471e290b20d3ee3323d308eb85dc4 to your computer and use it in GitHub Desktop.
Save AlmightyOatmeal/372471e290b20d3ee3323d308eb85dc4 to your computer and use it in GitHub Desktop.
Fun with Python and UTF-8 files! Works on Python 2.7.x.
import codecs
import logging
import os
logger = logging.getLogger(os.path.splitext(os.path.basename(__file__))[0])
# NOTE: Python 3.x doesn't have a `unicode()` built-in function.
def read_utf8_file(path, bom=False):
"""Read a UTF-8 encoded file.
:param path: Relative, or absolute, path of the file to read.
:type path: str or unicode
:param bom: (optional) Read a file and expect a UTF-8 BOM if True otherwise read UTF-8 without BOM. (default: False)
:type bom: bool
:return: Data or None
:rtype: str, unicode, or None
"""
if bom:
enc = 'utf-8-sig'
else:
enc = 'utf-8'
try:
with codecs.open(path, 'r', enc) as f:
return unicode(f.read().strip())
except Exception as err:
logger.exception(err)
return None
def write_utf8_file(path, data, overwrite_existing=False, bom=False):
"""Write a UTF-8 encoded file.
:param path: Relative, or absolute, path of the file to write.
:type path: str or unicode
:param data: Data to write.
:type data: *
:param overwrite_existing: (optional) If the file, overwrite it if set to True otherwise do not write.
(default: False)
:param bom: (optional) Write a UTF-8 BOM if True otherwise write UTF-8 without BOM. (default: False)
:type bom: bool
:return: True if successful else False.
:rtype: bool
"""
if bom:
enc = 'utf-8-sig'
else:
enc = 'utf-8'
if os.path.exists(path):
logger.warning('Output file already exists.')
if not overwrite_existing:
logger.warning('Overwrite existing is disabled.')
return False
try:
with codecs.open(path, 'w', enc) as f:
f.write(data)
except Exception as err:
logger.exception(err)
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment