Tengo una solución que he usado en la producción de 2 años. No bastante, pero funciona confiablemente. En la secuencia de comandos de py es llamar un archivo por lotes que obtiene una cadena de entrada del usuario y escribe en un archivo de texto temporal. Luego python lee ese archivo.
El archivo por lotes (getInput.bat):
@ echo off
REM Takes a single line of user input and writes it to an ascii file. More details below.
REM (See also getChoice.bat which takes a single keystroke and doesn't require "ENTER, control-Z and ENTER"
echo.
echo ==== GETINPUT.BAT =============================================
echo.
echo USER INPUT: respond with a line of text.
echo If you make a mistake, simply retype it -- this tool
echo saves just one line: the last _non-blank_ line entered.
echo.
echo To finish, press ENTER, control-Z and ENTER
echo.
echo.
echo *** %1
echo.
REM A blank input (eg: ^Z ENTER) leaves the output file unchanged, so
REM need to set a default string
SET USERINPUT=NULL
REM Closing the DOS window without input also leaves the output file unchanged, so need also
REM to overwrite the old file -- otherwise previous input is used. Note that the python
REM code below explicitly checks for "NULL"
echo NULL > B:\users\%username%\userinput.txt
REM Get userinput and send it to an environment variable.
FOR /F "tokens=*" %%A IN ('TYPE CON') DO SET USERINPUT=%%A
REM Send the value of the environment variable to a file, for python to read.
echo %USERINPUT% > B:\users\%username%\userinput.txt
El código de python:
import getpass
import subprocess
username = getpass.getuser()
userInputFile = "B:/users/" + username + "/userinput.txt"
msgarg = "Type row and column of the Cell. Numbers only, separated by a space."
# (use doublequotes so CMD sees it as one argument)
# run the batch file -- which prompts for input and writes it to a temp file
subprocess.call([r"B:\software\utilities\getInput.bat",msgarg])
# read the user's input from that file
with open(userInputFile) as inputFile:
for line in inputFile:
if line:
userInput = line.rstrip()
break # stop at the first line read
if line == "NULL":
# do something to respond to NULL input, e.g. ...
print = "Aborting process based on user input (" + userInput + ")"