A
A
alekssamos2020-07-12 14:18:51
Python
alekssamos, 2020-07-12 14:18:51

How to automate a console program?

There is a console program that, when launched, expects input (pressing a number) and Enter. T their mode does not work, configuration error. Can it be automated?
How to write correctly? I present it like this

import time
# ...
h = process.open("progname")
time.sleep(10)
h.write("2\n")
time.sleep(30)
h.write("1\n")
sleep(5)
h.close()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alekssamos, 2020-07-25
@alekssamos

from subprocess import Popen, PIPE, STDOUT
fil = "\n" * 10
p = Popen(['python', 'prog.py'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
p.communicate("1\n2" + fil)[0]

If there is another request (input), and it does not return anything, then an EOFError will be thrown, so I made the fil variable.
And it turns out that with such a launch, he does not expect anything.
It seems to work.
Just need to accurately calculate how much data will be requested.
If it doesn't work, add shell=True.
p = Popen(['python', 'prog.py'], stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=True)

But all the same, with long-term interaction: question1 - answer1, question2 - answer2 ... EOFError occurs and it is impossible to interact line by line.

S
Soska_VLG, 2020-07-12
@Soska_VLG

Try using schedule
https://schedule.readthedocs.io/en/stable/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question