1

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

    )
)
4
  • Is there some reason you don't want to write your testing code in python? There's very few things you can do in a batch file that you can't do in python. Commented Oct 29, 2014 at 23:00
  • @FredS I wouldn't even touch batch if they didn't ask me to do it in batch file, shrug Commented Oct 29, 2014 at 23:03
  • Well, I don't know who "they" are, but you could use a very simple batch file to launch "test_S_bank.py". Then that python file reads your test case input file and passes the values to your code being tested and checks the results. Just use the batch file to start up your test. Commented Oct 29, 2014 at 23:08
  • ditto @Fred's question. Do you know if your Python program accepts arguments? Try doing python.ex /? or pyton.exe /help That might help if it returns any help but is not conclusive if it does not. Commented Oct 29, 2014 at 23:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, it worked very well, just one more question, is <"%%~a" means use this as input for the following command?
@user3514453, yes, %%~a is the file being referenced by the for loop (without quotes if present) and the redirection operator < assigns the input stream for the command being processed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.