title: Sending to the stdin of a program in python3
body: <p>I have to files, <strong>main.py</strong> and <strong>child.py</strong>.</p>
  
  <p>I am trying to send a string to the stdin of <strong>main.py</strong>.</p>
  
  <p>This is my incomplete code:</p>
  
  <h1>main.py</h1>
  
  <pre><code>from subprocess import *
  import time
  
  def main():
      program = Popen(['python.exe'. 'child.py', 'start'])
      while True: #waiting for'1' to be sent to the stdin
          if sys.stdin == '1':
              print('text)
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <h1>child.py</h1>
  
  <pre><code>import sys
  
  if sys.argv[1] == 'start':
      inp = input('Do you want to send the argument?\n').lower()
      if inp == 'no':
          sys.exit()
      elif inp == 'yes':
          #Somehow send '1' to the stdin of 1.py while it is running
  </code></pre>
  
  <p>I have no idea how to do this.</p>
  
  <p>I am running windows 10 with python 3.5.1</p>
  
  <p>-Thanks</p>
  
  <p>EDIT:
  When I am sending the argument back to main.py, I can not re-open the program. os.system re-opens the program which is not useful in my case.</p>
  
  <p>These programs are a small demo of what I am trying to do. In my actual program, I am not able to do that as the two programs are "communicating" with each other an need to be open at all times.</p>
  
  <p>What I need answered is a way to send an argument to main.py perhaps using stdin but when I am sending my argument, It can not re-open the program. Some examples like os.system re-open the program which is not what I am trying to do. I need main.py open at all times.</p>
  
  <p>I have my new current code which is not working. A window pops up and then closes.</p>
  
  <h1>main.py</h1>
  
  <pre><code>from subprocess import Popen, PIPE, STDOUT
  x = Popen(['python.exe', '2.py', 'start'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
  while x.poll() is None:
      if b'Do you want to send the argument?' in x.stdout.read():
          x.stdin.write(b'yes\n')
  </code></pre>
  
  <h1>child.py</h1>
  
  <pre><code>import sys
  import time
  time.sleep(1)
  if 1 = 1:
      inp = input('Do you want to send the argument?\n').lower()
      if inp == 'no':
          sys.exit()
      elif inp == 'yes':
          sys.stdout.write('1')
          sys.stdout.flush()
  </code></pre>
  
  <p>That is my code.</p>