简体   繁体   中英

Passing inputs from a text file to python program using batch file

My python program asks for several inputs in an order and determine what output is needed. It works fine when using under python shell.

Now I'm trying to use windows batch file to create a testing program, which will read through text files and take contents from each text file as inputs for each test case.

I'm kinda new to batch files, so I'm not sure how to give python inputs inside this batch file. I tried to pass arguments by doing:

python.exe S_bank.py input

but then it just pop up the command line window without any inputs.

here is what I got so far(which doesn't work at all):

@echo off

setlocal enabledelayedexpansion

set line=0

for /r %%A in (*.txt) do (

    echo running test %%A >> frontendlog.txt
    start "C:\Python27\python.exe" "Z:\personal\test\S_bank.py"

    for /F %%i in (%%A) do (
        "Z:\personal\test\S_bank.py" %%i >> frontendlog.txt

    )
)

If your python code "asks" for input, the simplest way to automate it with batch is to prepare an answer text file for each of the cases to test, with a line for each of the prompts that the python program will use to retrieve information. Then iterate over the list of input files calling the python program with the answers file piped or redirected to it, so, the information is retrieved from the pipe insted of the console

So, for a simple code like

test.py

input_var1 = raw_input("Enter something: ")
input_var2 = raw_input("Enter something: ")
print ("you entered: " + input_var1 + ", " + input_var2) 

And answer files as

file1.txt

one
two

file2.txt

three
four

You will have a batch file as

@echo off
    setlocal enableextensions disabledelayedexpansion
    for %%a in ("file*.txt") do (
        <"%%~a" "C:\Python27\python.exe" "c:\somewhere\test.py"
    )

That is, for each answer file, call the python program, but redirect the answer file as input stream to the program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM