Directly write to encrypted file in Python
<p>In python, using the gnupg package, is it possible to take a value in memory, then write it an encrypted file rather than writing to file THEN encrypting?  </p>

<p>I was hoping something like this would work:</p>

<pre><code>import gnupg

gpg = gnupg.GPG(gnupghome='keydirectory')

l = [['a', '1'], ['b', '2'], ['c', '3']]

gpg.encrypt_file(l, recipients=['test@test.com'], output='encryptedfile.asc')
</code></pre>

<p>I was hoping there was a write concept like this to loop over line-by-line, but I can't find one.  </p>

<pre><code>with open('regularfile.txt', 'w') as file:
  for i in l:
    file.write(i)
</code></pre>

<p>Ideally, I could connect to a database and output a file by writing directly.  </p>

<p>Just use the <code>gpg.encrypt()</code> function instead of <code>encrypt_file()</code> and then write to a file.</p>

python gnupg