Sreevalsa E
Sreevalsa E

Reputation: 986

How do I pass a string into subprocess module?

I have an executable which will prompt for different options Eg; Executable: sample.exe On running sample.exe, I get a prompt to enter "help", "display", "quit" options.

I need to enter all these and check the output whether it has a valid output.

I am using subprocess to run the sample.exe.

I can pass the input using subprocess.communicate(input='help')

But other than quit all the option will not end the console and still waits for the input.

Tried code:

p_run = subprocess.Popen(['sample.exe'], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p_run_out = p_run.communicate(input=b'help')
print(p_run_out)

Here it stuck in the p_run_out = p_run.communicate(input=b'help') as sample.exe waits for another input. It only ends when we pass quit

How can I pass next command? How to get the output of previous command('help') ?

Upvotes: 0

Views: 70

Answers (1)

amsh
amsh

Reputation: 3377

You can pass multiple commands and read the output of the first command only.

Updated code:

p_run = subprocess.Popen(['sample.exe'], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p_run_out = p_run.communicate(input=b'help\nquit')[0]
print(p_run_out)

Upvotes: 1

Related Questions