Skip to content

Instantly share code, notes, and snippets.

@davismj
Created February 29, 2016 02:50
Show Gist options
  • Save davismj/5ceb7f307f4038ba3264 to your computer and use it in GitHub Desktop.
Save davismj/5ceb7f307f4038ba3264 to your computer and use it in GitHub Desktop.
get_text_from_file
def get_text_from_file(file_path):
value = None
try:
with open(file_path, mode='r') as file:
value = file.read()
except UnicodeDecodeError:
try:
with open(file_path, mode='rb') as file:
value = file.read()
except:
raise ValueError('Cannot read binary file {0}'.format(file_path))
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment