-1

I have a batch script which is generating a configuration file after its execution, the configuration file generated has some data inside it. When i execute it from command prompt as follows i get the desired output :

C:/> start.bat

but if i try to execute from python script as follows or even if i double click and execute it i do get the configuration file but it does not contain any required data it just have '0' inside it:

import subprocess as sp
p= sp.Popen("C:/pathtobatch/start.bat",stdin=sp.PIPE, stdout = sp.PIPE, stderr=sp.PIPE)

Inside the batch script(start.bat) i am actually executing a python script and retrieving its data to a configuration file as follows:

python C:\inetpub\ftproot\sample.py > log.txt
set  myvar =< log.txt
del log.txt
echo %errorlevel% %myvar% >C:\inetpub\ftproot\config.txt

I need to execute the batch(start.bat) from the python script and generate the config.txt file having the required data. So, how shall i do that. Why the start.bat is not working fine if i double click it and why is it working fine if i am executing it from command prompt.

Learner
  • 453
  • 13
  • 29
  • put `pause` at the end of bat and try double clicking. – WhoAmI Mar 28 '16 at 06:48
  • @WhoAmI no it does not woking by adding pause also.:/ – Learner Mar 28 '16 at 07:10
  • 2
    Specify full paths, everywhere. This pointless question which is about configuration of the execution environment is asked over and over again. Specify exactly what you want to happen. `%userprofile%\desktop` will usually be the user's desktop. –  Mar 28 '16 at 07:22
  • How are you handling the job's stdin/stdout/stderr in the Python? Are you waiting for the job to complete before exiting the Python program? – cdarke Mar 28 '16 at 07:27
  • 2
    `set /P myvar=< log.txt`; read _entire_ `set /?` and/or http://ss64.com/nt/set.html – JosefZ Mar 28 '16 at 07:33
  • 2
    Please read [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](http://stackoverflow.com/a/26388460/3074564) to understand why [JosefZ](http://stackoverflow.com/users/3439404/josefz) suggested the command __SET__ without spaces around the operators. – Mofi Mar 28 '16 at 08:44

1 Answers1

1

Is it possible that you get '0' as it is the value of %errorlevel%?

I found two issues in your case:

1) batch file - can you please update your batch file to something like:

for /f %%i in ('python C:\inetpub\ftproot\sample.py') do set myvar=%%i
echo %errorlevel% %myvar% >C:\inetpub\ftproot\config.txt

Credit for the idea of setting variable: https://stackoverflow.com/a/2340018/5088142

2) python script - can you please update your python script to be:

import os
os.system("C:/pathtobatch/start.bat")
Community
  • 1
  • 1
Yaron
  • 10,166
  • 9
  • 45
  • 65