title: How to override sys.stdin to duplicate the input stream
body: <p>I'm writing a Python 3.6 program on Windows to record all the <strong>input</strong> to a subprocess for backup. (I cannot modify the subprocess code as the real program is a commercial product (exe file) and I don't have the source code.) </p>
  
  <p>I tried the following code, and it doesn't work. The text only shows in the terminal, and the txt files used for logging are empty.</p>
  
  <p><strong>Main.py</strong>:</p>
  
  <pre><code>import subprocess
  import sys    
  
  class dup_stream():
      def __init__(self, original_stream, file):
          self.original_stream = original_stream
          self.log = open(file, 'a')
  
      def write(self, message):
          self.log.write(message)
          self.original_stream.write(message)
  
      def fileno(self):
          return self.original_stream.fileno()
  
  
  completed_process = subprocess.run('python Hello.py',
                                     stdin=dup_stream(sys.stdin, 'stdin.txt'),
                                     stderr=dup_stream(sys.stderr, 'stderr.txt'),
                                     stdout=dup_stream(sys.stdout, 'stdout.txt'))
  </code></pre>
  
  <p><strong>Hello.py</strong>:</p>
  
  <pre><code>name = input("Your name:") # e.g. "Jane Doe"
  print('Hello,',name)
  </code></pre>
  
  <hr>
  
  <p>Expected result:</p>
  
  <p>In terminal:</p>
  
  <pre><code>Your name: Jane Doe
  Hello, Jane Doe
  </code></pre>
  
  <p>stdin.txt:</p>
  
  <pre><code>Jane Doe
  </code></pre>
  
  <p>stdout.txt:</p>
  
  <pre><code>Your name:
  Hello, Jane Doe
  </code></pre>
  
  <p>I've asked a more general question before (<a href="https://stackoverflow.com/questions/53810417/capture-inputs-to-subprocess-run">Capture inputs to subprocess.run()</a>), but there aren't any practical answer.</p>