问题描述
我的python程序按顺序要求输入多个输入,并确定需要什么输出.在python shell下使用时,效果很好.
My python program asks for several inputs in an order and determine what output is needed. It works fine when using under python shell.
现在,我正在尝试使用Windows批处理文件创建一个测试程序,该程序将读取文本文件并从每个文本文件中获取内容作为每个测试用例的输入.
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.
我对批处理文件有点陌生,所以我不确定如何在此批处理文件中提供python输入.我试图通过以下方式传递参数:
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
)
)
推荐答案
如果您要输入的python代码"asks",最简单的批处理自动化方法是为每个要测试的案例准备一个答案文本文件, python程序将用于检索信息的每个提示都带有一行.然后遍历调用python程序的输入文件列表,并通过管道将其重定向或重定向到答案文件,以便从控制台的管道中检索信息.
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)
将文件答复为
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"
)
也就是说,对于每个答案文件,请调用python程序,但将答案文件作为输入流重定向到程序
That is, for each answer file, call the python program, but redirect the answer file as input stream to the program
这篇关于使用批处理文件将输入从文本文件传递到python程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!