Skip to content Skip to sidebar Skip to footer

How Can I Simulate A Key Press In A Python Subprocess?

The scenario is, I have a Python script which part of it is to execute an external program using the code below: subprocess.run(['someExternalProgram', 'some options'], shell=True)

Solution 1:

from subprocess import Popen, PIPE

p = Popen(["someExternalProgram", "some options"], stdin=PIPE, shell=True)
p.communicate(input=b'\n')

If you want to capture the output and error log

from subprocess import Popen, PIPE

p = Popen(["someExternalProgram", "some options"], stdin=PIPE, stdout=PIPE, stderr=PIPE shell=True)
output, error = p.communicate(input=b'\n')

Post a Comment for "How Can I Simulate A Key Press In A Python Subprocess?"