pexpect

Multiple inputs and outputs in python subprocess communicate

橙三吉。 提交于 2019-11-26 04:55:02
问题 I need to do something like this post, but I need to create a subprocess that can be given input and give output many times. The accepted answer of that post has good code... from subprocess import Popen, PIPE, STDOUT p = Popen([\'grep\', \'f\'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) grep_stdout = p.communicate(input=b\'one\\ntwo\\nthree\\nfour\\nfive\\nsix\\n\')[0] print(grep_stdout.decode()) # four # five ...that I would like to continue like this: grep_stdout2 = p.communicate(input=b\

Run command and get its stdout, stderr separately in near real time like in a terminal

大城市里の小女人 提交于 2019-11-26 02:37:12
问题 I am trying to find a way in Python to run other programs in such a way that: The stdout and stderr of the program being run can be logged separately. The stdout and stderr of the program being run can be viewed in near-real time, such that if the child process hangs, the user can see. (i.e. we do not wait for execution to complete before printing the stdout/stderr to the user) Bonus criteria: The program being run does not know it is being run via python, and thus will not do unexpected