Sample code for 30+ languages & platforms
Chilkat2-Python

Get Contents of File as Base64

See more Encryption Examples

Demonstrates how to read the contents of a file and convert to a base64 string.

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

bd = chilkat2.BinData()

success = bd.LoadFile("qa_data/jpg/starfish.jpg")
if (success == False):
    print("Failed to load file.")
    sys.exit()

print(bd.GetEncoded("base64"))

# If you want mult-line base64:
print("--")
print(bd.GetEncoded("base64_mime"))

# If you want hex..
print("--")
print(bd.GetEncoded("hex"))

# etc.