python调用批处理并传入命令_使用批处理文件将输入从文本文件传递到python程序...

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 (

)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值