title: How to enter a password into the terminal password prompt in Python
body: <p>I am trying to make a simple Python script that enters a given password in the command line after using the 'su' command (or any other command which requires administrator privileges or simply requires a password in order to be executed). </p>
  
  <p>I tried to use the Subprocess module for this as well as pynput, but haven't been able to figure it out.</p>
  
  <pre><code>import subprocess
  import os
  
  # os.system('su') # Tried using this too
  
  process = subprocess.Popen('su', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  process.stdin.write(b"password_to_enter")
  print(process.communicate()[0])
  process.stdin.close()
  </code></pre>
  
  <p>I was expecting this to enter 'password_to_enter' in the given password prompt after typing the 'su' command, but it didn't. I tried giving it the correct password as well but still did not work.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>PS: I am on Mac</p>