import os
import argparse
import base64
def encode_file(input_file, output_file):
"""Encodes the content of a file using base64 and writes it to a new file."""
try:
with open(input_file, 'r') as f_in:
file_content = f_in.read().encode('utf-8') # Read file content and encode to bytes
encoded_bytes = base64.b64encode(file_content) # Encode the bytes using base64
encoded_string = encoded_bytes.decode('utf-8') # Decode the bytes to string
with open(output_file, 'w') as f_out:
f_out.write(encoded_string) # Write encoded string to the output file
print(f"File '{input_file}' encoded to '{output_file}'")
except FileNotFoundError:
print(f"Error: File '{input_file}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
def main():
"""Parses command line arguments and encodes the specified files."""
parser = argparse.ArgumentParser(description="Encode files using base64.")
parser.add_argument("input_file", help="Path to the input file.")
parser.add_argument("output_file", help="Path to the output file.")
args = parser.parse_args()
encode_file(args.input_file, args.output_file)
if __name__ == "__main__":
main()
Add your comment