0

I'm calling python script as follows:
start python file.py How can I get the output in a variable?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Omkar J
  • 53
  • 10
  • @MA1 The question says "batch file", so I would assume it's talking about Windows CMD.exe batch files. That question is about bash. – Weeble Oct 16 '14 at 09:13
  • This might be helpful: http://stackoverflow.com/questions/2768608/batch-equivalent-of-bash-backticks – Weeble Oct 16 '14 at 09:15

1 Answers1

0

Using start complicates matters. It starts a program running and doesn't wait for it to finish. You probably don't want to use start if you also want to wait for the program to finish and use its output.

Since other programs can't "reach into" your batch process and change its variables, you need to use some mechanism to output the information from the program and a mechanism to read that information and turn it into variables in the batch file.

If your output mechanism is just for the program to print to standard output (note: you should not use start in this case, since it will send standard output to a new window instead), you can redirect it to a file and then read it into a variable. Batch files don't make this easy, but it is possible: Batch equivalent of Bash backticks

Alternatively, if you can change the Python program, you can also take a slightly simpler approach - you can have the Python script write out a batch file directly, then have the first batch file call that one. E.g.:

python script.py
call generated.bat
echo %MYVAR%

Where the Python script creates a file generated.bat:

set MYVAR=This is an example
Community
  • 1
  • 1
Weeble
  • 17,058
  • 3
  • 60
  • 75