1

I am trying to automatically press enter after I execute a command in command prompt, for example, I run....

    d:\myunit> codecov instrument -ip

And it outputs:

Warning: It is recommended when instrumenting code that you use the sub-unit option for 'codecov' to avoid instrumentation being inserted in the source code in your development unit. Press 'enter' to continue with-out using a sub-unit.

If that command is actually executed from a python script(using os.system('codecov instrument -ip')). How can I make the same script also press enter so that the script can continue without user input? Currently I have been trying to use a subprocess but am not sure if it is the best way to go about it and have not gotten it to work.

John S
  • 21
  • 4
  • 1
    Use `subprocess` and write a `\n` to the process's `stdin`. See https://stackoverflow.com/questions/163542/python-how-do-i-pass-a-string-into-subprocess-popen-using-the-stdin-argument – cdarke Feb 20 '18 at 22:26
  • 1
    You may need to flush `stdin` as well. Also, if you need continuous communication, you probably need separate threads for `stdin` and `stdout`. Something along the lines of my answer at https://stackoverflow.com/a/48777349/7738328 – JohanL Feb 20 '18 at 23:07

1 Answers1

1
from subprocess import Popen, PIPE
import os, sys, subproces

read, write = os.pipe() 
os.write(write, b"\n")
os.close(write)
subprocess.check_call('codecov instrument -ip', stdin=read, shell=True)

This is what I was able to use as the solution.

John S
  • 21
  • 4