Skip to content

Instantly share code, notes, and snippets.

@donutheist
Last active October 2, 2025 18:52
Show Gist options
  • Select an option

  • Save donutheist/07a81054218462f7bf182cf0ac394d82 to your computer and use it in GitHub Desktop.

Select an option

Save donutheist/07a81054218462f7bf182cf0ac394d82 to your computer and use it in GitHub Desktop.
Python pass input to to use as stdin
import subprocess
def execute_command(command=None, stdin=None):
proc = subprocess.Popen(
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# Use communicate to send input and capture both stdout/stderr
stdout, stderr = proc.communicate(input=stdin)
rc = proc.returncode
return rc, stdout, stderr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment