11

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?

2
  • 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. Commented Sep 14, 2015 at 8:00
  • @TheBlackCat Thank you. I'm sorry this problem. Commented Sep 14, 2015 at 8:19

1 Answer 1

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!

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

5 Comments

Wow.. It so Very Simple.
As far I have understood this code :D this Code > Output saved first print so i removed otiose my comment. Thank you for answer. I owe you, pss.
You are welcome. Happy coding :)
but this ">Outbut" saved only the first print. I need it to save the last print/ or last value result, how can I do it?
@ZuiZui From your first Python script, create a temporary text file and write your last print's string into that text file. And then simply access that text file to retrieve the string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.