Skip to content

Instantly share code, notes, and snippets.

@hxtree
Last active November 19, 2019 20:35
Show Gist options
  • Select an option

  • Save hxtree/8392f93973db1bf8291db83ae12c4128 to your computer and use it in GitHub Desktop.

Select an option

Save hxtree/8392f93973db1bf8291db83ae12c4128 to your computer and use it in GitHub Desktop.
Python Make Encrypted Copy of Files With Extension
# GPG (GNU Privacy Guard)
# Make Encrypted Copy of Files With Extension
import os
input_dir = '/share/backups/'
output_dir = '/share/backups/encrypted/'
passphrase = '/share/backups/passphrase.txt'
extensions = {'.zip','.bak'}
path_matches = []
# get paths of files matching extension recusively
for root, dirs, files in os.walk(input_dir):
for file in files:
for i, extension in enumerate(extensions):
if file.endswith(extension):
path_matches.append(os.path.join(root, file))
# encrypt each file found to new directory
for file_input_path in path_matches:
file_output_path = file_input_path.replace(input_dir, output_dir, 1)
file_output_dir = os.path.dirname(file_output_path)
# make directory, if not exist
if not os.path.exists(file_output_dir):
print('Make folder ' + file_output_dir)
os.makedirs(file_output_dir)
print('Encrypting ' + file_input_path + ' to ' + file_output_path);
# process as batch to automatically rewrite files
os.system('gpg --cipher-algo AES256 --batch --yes --passphrase ' + passphrase + ' --output ' + file_output_path + '.gpg --symmetric ' + file_input_path)
INSERT YOUR PASSPHRASE HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment