'Auto-enter a value when prompted from a Python Script

Say I have access to a Python script (of which I didn't create) that prompts for a user input and then runs some processing tasks based off the input before outputting an Excel spreadsheet. To speed things up, I happen to have a "list" of user inputs I want to pass to the script so that it creates ALL spreadsheets in one go, rather than me having to re-run the script each time.

Without altering the main script (risk of creating new problems), could this be achieved using an MS-DOS batch file? Or is there an alternative way this could be achieved with another Python script(s)?

For simplicity, here are 2 examples to better reference what I'd like to achieve.

hello.py

print("Hi There!")
print("What is your name?")
myName = input()
print("It is great to meet you, " + myName)
if myName == "Tim":
    print("I think we met at the Xmas party last year. Hope it's as good this year!")
elif myName == "Bob":
    print("I think we met at the New Year party last year. Hope it's as good this year!")
else:
    print("Speak soon, Bye!")

do.bat

cd "C:\Users\Tim\Desktop\Python\Test"
C:\Anaconda\envs\Tim\python.exe "C:\Users\Tim\Desktop\Python\Test\hello.py"
Tim
C:\Anaconda\envs\Tim\python.exe "C:\Users\Tim\Desktop\Python\Test\hello.py"
Bob
pause

The problem I'm encountering is when do.bat is ran, the hello.py still "listens" for a user input, rather than entering the value Tim as stated in the batch file. Therefore an entry is still required, although when the script completes, it then tries to run a command called Tim (of which fails).

I know this seems straightforward but I'm trying to perform this task with minimum effort as the main Python script takes considerable time to run and I don't want to hack it apart.



Solution 1:[1]

You can do this by piping echo'd input into the running script:

echo Tim | C:\Anaconda\envs\Tim\python.exe "C:\Users\Tim\Desktop\Python\Test\hello.py"
echo Bob | C:\Anaconda\envs\Tim\python.exe "C:\Users\Tim\Desktop\Python\Test\hello.py"
pause

Produces

path>echo Tim | C:\Anaconda\envs\Tim\python.exe "C:\Users\Tim\Desktop\Python\Test\hello.py"
Hi There!
What is your name?
It is great to meet you, Tim
Speak soon, Bye!

path>echo Bob | C:\Anaconda\envs\Tim\python.exe "C:\Users\Tim\Desktop\Python\Test\hello.py"
Hi There!
What is your name?
It is great to meet you, Bob
Speak soon, Bye!

echo Tim prints Tim to the STDOUT, but | pipes that into the STDIN of python.

You can put @echo off at the top to make the output a little less noisy too.


And if you want to supply multiple inputs for say a script like this:

in1 = input("1")
in2 = input("2")
print(in1, "+", in2)

You can group the echos and pipe them together:

@echo off
:: I'm using simpler executable/script paths here for brevity
(echo One
 echo Two) | python test.py

(echo Three
 echo Four) | python test.py

Produces:

12One  + Two
12Three  + Four

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1