title: Encrypting many PDFs by python using PyPDF2
body: <p>I am trying to make a python program which loops through all files in a folder, selects those which have extension '.pdf', and encrypt them with restricted permissions. I am using this version of PyPDF2 library:
  <a href="https://github.com/vchatterji/PyPDF2" rel="nofollow noreferrer">https://github.com/vchatterji/PyPDF2</a>. (A modification of the original PyPDF2 which also allows to set permissions). I have tested it with a single pdf file and it works fine. I want that the original pdf file should be deleted and the encrypted one should remain with the same name.
  Here is my code:</p>
  
  <pre><code>import os
  import PyPDF2
  
  directory = './'
  
  for filename in os.listdir(directory):
      if filename.endswith(".pdf"): 
          pdfFile = open(filename, 'rb')
          pdfReader = PyPDF2.PdfFileReader(pdfFile)
          pdfWriter = PyPDF2.PdfFileWriter()
          for pageNum in range(pdfReader.numPages):
              pdfWriter.addPage(pdfReader.getPage(pageNum))
          pdfFile.close()
          os.remove(filename)
          pdfWriter.encrypt('', 'ispat', perm_mask=-3904)
          resultPdf = open(filename, 'wb')
          pdfWriter.write(resultPdf)
          resultPdf.close()
          continue
      else:
          continue
  </code></pre>
  
  <p>It gives the following error:</p>
  
  <pre><code>  C:\Users\manul\Desktop\ghh&gt;python encrypter.py
    Traceback (most recent call last):
    File "encrypter.py", line 9, in &lt;module&gt;
    pdfReader = PyPDF2.PdfFileReader(pdfFile)
    File "C:\Users\manul\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 1153, in __init__
    self.read(stream)
    File "C:\Users\manul\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 1758, in read
      stream.seek(-1, 2)
    OSError: [Errno 22] Invalid argument
  </code></pre>
  
  <p>I have some PDFs stored in 'ghh' folder on Desktop. Any help is greatly appreciated.</p>