Created
January 24, 2018 07:03
-
-
Save narbehaj/a44895c3abb66c26375faf7b775cecbf to your computer and use it in GitHub Desktop.
Encrypt files using Python GnuPG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################### | |
# pip3 install python-gnupg | |
########################### | |
import os | |
from gnupg import GPG | |
# If using Debian/Ubuntu, it should be under | |
# /home/USERNAME/.gnupg otherwise check manually | |
gnupg_home = '/home/narbeh/.gnupg' | |
recipient = '[email protected]' | |
dir_to_encrypt = '/tmp/test/' | |
dest_for_encrypt = '/tmp/encrypted/' | |
def encrypt(directory, file): | |
with open(os.path.join(directory, file), 'rb') as f: | |
out_file = os.path.join(dest_for_encrypt, directory[len(dir_to_encrypt):] + '.gpg') | |
gpg.encrypt_file(f, armor=False, recipients=[recipient], output=out_file) | |
def iter_files(): | |
for item_dir, sub_dir, files in os.walk(dir_to_encrypt): | |
for file in files: | |
encrypt(item_dir, file) | |
if __name__ == '__main__': | |
gpg = GPG(gnupghome=gnupg_home) | |
iter_files() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment