title: PyPDF2: writing output to stdout fails with python3
body: <p>I am trying to use Python 3.7.2 with PyPDF2 1.26 to select some pages of an input PDF file and write the output to stdout (the actual code is more complicated, this is just a MCVE):</p>
  
  <pre><code>import sys
  from PyPDF2 import PdfFileReader, PdfFileWriter
  
  input = PdfFileReader("example.pdf")
  output = PdfFileWriter()
  output.addPage(input.getPage(0))
  
  output.write(sys.stdout)
  </code></pre>
  
  <p>This fails with the following error:</p>
  
  <pre><code>UserWarning: File &lt;&lt;stdout&gt;&gt; to write to is not in binary mode. It may not be written to correctly. [pdf.py:453]
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/usr/lib/python3.7/site-packages/PyPDF2/pdf.py", line 487, in write
      stream.write(self._header + b_("\n"))
  TypeError: write() argument must be str, not bytes
  </code></pre>
  
  <p>The problem seems to be that <code>sys.stdout</code> is not open in binary mode. As some of the answers suggest, I have tried the following:</p>
  
  <pre><code>output.write(sys.stdout.buffer)
  </code></pre>
  
  <p>This fails with the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/usr/lib/python3.7/site-packages/PyPDF2/pdf.py", line 491, in write
      object_positions.append(stream.tell())
  OSError: [Errno 29] Illegal seek
  </code></pre>
  
  <p>I have also tried the answer from <a href="https://stackoverflow.com/q/50476200/2170917">Changing the way stdin/stdout is opened in Python 3</a>:</p>
  
  <pre><code>sout = open(sys.stdout.fileno(), "wb")
  output.write(sout)
  </code></pre>
  
  <p>This fails with the same error as above.</p>
  
  <p>How can I use the PyPDF2 library to output a PDF to standard output?</p>
  
  <p>More generally, how do I correctly switch <code>sys.stdout</code> to binary mode (akin to Perl's <code>binmode STDOUT</code>)?</p>
  
  <p>Note: There is no need to tell me that I can open a file in binary mode and write the PDF to that file. That works; however, I specifically want to write the PDF to stdout.</p>