satya chandra satya chandra - 2 months ago 6
Bash Question

How to store output of a python program in command prompt?

I'm passing run time variable from command prompt to python to execute there (im=n python). Now I want to store the result of python program and use those results back into command prompt. Sample is as follows

set input=
set /P input=Enter Layer Name:%=%
C:\Python27\python.exe F:\xampp\htdocs\flood_publish\projection_raster.py %input%


I'm passing user input string to python program from command prompt as above.
How use the result of python program back into command prompt

Answer

Let me know if this helps.

Code of python file (c.py):

import sys

print('You passed ',sys.argv[1])

Windows Batch Code (a.bat) :

@echo off

set input=

set /P input=Enter Layer Name:%=%

(python c.py %input%) > tmp.txt

set /P output=<tmp.txt

echo %output%

Output of batch code:

C:\Users\dinesh_pundkar\Desktop>a.bat
Enter Layer Name:Dinesh
You passed  Dinesh
C:\Users\dinesh_pundkar\Desktop>
Comments