title: How to encrypt a large dataset using python-gnupg without sucking up all the memory?
body: <p>I have a very large text file on disk. Assume it is 1 GB or more. Also assume the data in this file has a <code>\n</code> character every 120 characters.</p>
  
  <p>I am using <a href="https://pythonhosted.org/python-gnupg/" rel="nofollow">python-gnupg</a> to encrypt on this file.
  Since the file is so large, I cannot read the entire file into memory at one time.</p>
  
  <p>However, the <code>gnupg.encrypt()</code> method that I'm using requires that I send in all the data at once -- not in chunks. So how can I encrypt the file without using up all my system memory?</p>
  
  <p>Here is some sample code:</p>
  
  <pre><code>import gnupg
  gpg = gnupg.GPG(gnupghome='/Users/syed.saqibali/.gnupg/')
  
  for curr_line in open("VeryLargeFile.txt", "r").xreadlines():
      encrypted_ascii_data = gpg.encrypt(curr_line, "recipient@gmail.com")
      open("EncryptedOutputFile.dat", "a").write(encrypted_ascii_data)
  </code></pre>
  
  <p>This sample produces an invalid output file because I cannot simply concatenate encrypted blobs together into a file.</p>