Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get return value ( string ) from called Python by BatchFile [duplicate]

I need to have some help.

I used python33 on Windows OS.

I want result it.

Call Python in Batch File. ( python Testing.py %arg1% %arg2% )

python is do return string value. ( return('END') or exit('END') )

and I want set BatchFile Variable ( SET returnValue = python Testing.py %arg1% %arg2% )

and Use Variable %returnValue%

I tried search key word. 'python return value to batch file'

I found this hint.
( Python ) sys.exit(number) So Called %ErrorLevel% in BatchFile.

But this Sample can integer. don't apply string.

Of course, Python can make string in file. and Batch File can Load File .

or Also Environment Variable can.

But I want different method.

I want just Python do Return value and How use the Return Value in Batch File .

How can I do that?

like image 864
Kibum Ko Avatar asked Sep 14 '15 07:09

Kibum Ko


1 Answers

As far I have understood your question. You would like to execute a python script from a batch file and then would like to save the output of the python script to a variable in the batch file.

Here is my way (there could be a better way to do this). I am simply executing the python script test2.py from my batch file and then I save the output of the python script to a temporary file called as Output later I read this file to a batch file variable named as MYVAR and then I am deleting the temp file Output.

The batch code is provided below:

@ECHO OFF
test2.py > Output
SET /p MYVAR=<Output
ECHO %MYVAR%
PAUSE
DEL Output

The python file test2.py contains the following code:

print "Python"

The batch file & the python file are in the same directory!

Output:

Upon executing the batch file you shall see this in the console:

Python
Press any key to continue . . .

Hope it helps!

like image 52
ρss Avatar answered Oct 21 '22 01:10

ρss