How to Make Python Script Press 'Enter' When Prompted on Shell

How to make python script press 'enter' when prompted on Shell

You can use subprocess.Popen and subprocess.communicate to send input to another program.

For example, to send "enter" key to the following program test_enter.py:

print "press enter..."
raw_input()
print "yay!"

You can do the following:

from subprocess import Popen, PIPE
p = Popen(['python test_enter.py'], stdin=PIPE, shell=True)
p.communicate(input='\n')

You may find answer to "how do i write to a python subprocess' stdin" helpful as well.

How to make python script type when prompted on shell

Script to call:

print("write something here")
inserted_string = input()
print("You wrote: " + inserted_string)

Caller script:

from subprocess import Popen, PIPE
import sys

print("custom insert here...")
inserted1 = input()

if len(sys.argv) > 1:
inserted2 = sys.argv[1]
else:
inserted2 = 'nothing here'

p1 = Popen(['python script_to_call.py'], stdin=PIPE, shell=True)
p1.communicate(input=b'standard string\n')

print(" --- ")

p2 = Popen(['python script_to_call.py'], stdin=PIPE, shell=True)
p2.communicate(input=inserted1.encode('utf-8') + b'\n')

print(" --- ")

p3 = Popen(['python script_to_call.py'], stdin=PIPE, shell=True)
p3.communicate(input=inserted2.encode('utf-8') + b'\n')

Run command

python caller.py "string got from parameter"

Output:

custom insert here...
string got from input

write something here
You wrote: standard string
---
write something here
You wrote: string got from input
---
write something here
You wrote: string got from parameter

Automatically do keypress enter when prompted in command prompt

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.

Press enter as command input

You need to supply a newline on the installer's stdin.

One way:

subprocess.Popen('sh abc.sh < a_file_that_you_prepared.txt',
shell=True,
stdout=subprocess.PIPE)

Another, roughly equivalent to the first:

input_file = open('a_file_that_you_prepared.txt', 'r')
subprocess.Popen('sh abc.sh',
shell=True,
stdout=subprocess.PIPE,
stdin=input_file)
input_file.close()

Another way -- might work, might not:

subprocess.Popen('sh abc.sh < /dev/null',
shell=True,
stdout=subprocess.PIPE)

Third way -- can easily cause deadlock between your program and installer:

x = subprocess.Popen('sh abc.sh',
shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)

...

x.stdin.write('\n')

How to run a shell script without having to press enter/confirm s.th. inbetween

I finally found an easy and fast way for simulating a key press:

just install xdotool and then use the following code for simulating e.g. the enter key:

import subprocess
subprocess.call(["xdotool","key","Return"])

How do I wait for a pressed key?

In Python 3, use input():

input("Press Enter to continue...")

In Python 2, use raw_input():

raw_input("Press Enter to continue...")

This only waits for the user to press enter though.


On Windows/DOS, one might want to use msvcrt. The msvcrt module gives you access to a number of functions in the Microsoft Visual C/C++ Runtime Library (MSVCRT):

import msvcrt as m
def wait():
m.getch()

This should wait for a key press.


Notes:

In Python 3, raw_input() does not exist.

In Python 2, input(prompt) is equivalent to eval(raw_input(prompt)).

How can I simulate a key press in a Python subprocess?

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')

remember that the input has to be a bytes object



Related Topics



Leave a reply



Submit