Base64 file encode and decode with Python

From PedrosBrainDump
Revision as of 02:03, 12 October 2024 by 413vhcu1lq0463ob (talk | contribs) (Created page with "Encode code import base64 input_file_path = input('Input file > ') output_file_path = input('Output file > ') with open(input_file_path, 'rb') as file: file_data = file.read() encoded_data = base64.b64encode(file_data) with open(output_file_path, 'wb') as output_file: output_file.write(encoded_data) Decode code import base64 input_file_path = input('Input file > ') output_file_path = input('Output file > ') with open(input_file_path, 'r...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Encode code

import base64

input_file_path = input('Input file > ')
output_file_path = input('Output file > ')

with open(input_file_path, 'rb') as file:
    file_data = file.read()
    encoded_data = base64.b64encode(file_data)

with open(output_file_path, 'wb') as output_file:
    output_file.write(encoded_data)

Decode code

import base64

input_file_path = input('Input file > ')
output_file_path = input('Output file > ')

with open(input_file_path, 'rb') as encoded_file:
    encoded_data = encoded_file.read()
    decoded_data = base64.b64decode(encoded_data)

with open(output_file_path, 'wb') as output_file:
    output_file.write(decoded_data)