PASSPHRASE = '...'
in_fd, out_fd = os.pipe()
os.write(out_fd, PASSPHRASE)
os.close(out_fd)
cmd = 'gpg --passphrase-fd {fd} -c'.format(fd=in_fd)
with open('filename.txt', 'r') as stdin_fh:
    with open('filename.gpg', 'w') as stdout_fh:
        proc = subprocess.Popen(shlex.split(cmd), stdin=stdin_fh, stdout=
            stdout_fh)
proc.communicate()
os.close(in_fd)
Write to file descriptor 3 of a Python subprocess.Popen object