10

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?

Kibum Ko
  • 123
  • 1
  • 1
  • 6
  • What have you tried so far? stackoverflow isn't a code-writing service, you need to show what you have already tried and explain how what it does is different from what you want to do. – TheBlackCat Sep 14 '15 at 08:00
  • @TheBlackCat Thank you. I'm sorry this problem. – Kibum Ko Sep 14 '15 at 08:19

1 Answers1

19

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!

ρss
  • 5,115
  • 8
  • 43
  • 73