Skip to content

Instantly share code, notes, and snippets.

@allanaguilar
Forked from maniartech/file_to_base64.py
Created June 28, 2018 19:48
Show Gist options
  • Save allanaguilar/b9001715c09ba4747a11bbc2431c1aba to your computer and use it in GitHub Desktop.
Save allanaguilar/b9001715c09ba4747a11bbc2431c1aba to your computer and use it in GitHub Desktop.
A python function which converts file to base64 string.
def file_to_base64(file_path):
"""
A simple function which accepts the file_path and
converts and returns base64 string if specified file
exists. Returns blank string '' if file is not found.
"""
from os import path
if not path.exists(file_path):
return ''
return open(file_path, 'rb').read().encode('base64')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment