3

I have a basic batch file that takes user input:

@echo off
set /p Thing= Type Something: 
echo %Thing%
pause

However, I'd like to use a variable written in Python to pass into the batch file. Let's say just a string 'arg1' This is just a basic example, but I still cannot figure it out. The below code will run the batch process, but 'arg1' has no impact

import subprocess

filepath = r'C:\Users\MattR\Desktop\testing.bat'

subprocess.call([filepath, 'arg1'])

I have also tried p = subprocess.Popen([filepath, 'arg1']) but the batch file does not run in Python.

I have searched the web and SO, but none of the answers seem to work for me. Here are some links I've also tried: Example 1, Example 2. I've also tried others but they seem fairly specific to the user's needs.

How do I start passing Python variables into my batch files?

Community
  • 1
  • 1
MattR
  • 4,887
  • 9
  • 40
  • 67

2 Answers2

8

Your subprocess likely needs to run with a shell if you want bash to work properly

Actual meaning of 'shell=True' in subprocess

so

subprocess.Popen([filepath, 'arg1'], shell=True)

If you want to see the output too then:

item = subprocess.Popen([filepath, 'arg1'], shell=True, stdout=subprocess.PIPE)
for line in item.stdout:
     print line

As a further edit here's a working example of what you're after:

sub.py:

import subprocess
import random


item = subprocess.Popen(["test.bat", str(random.randrange(0,20))] , 
                         shell=True, stdout=subprocess.PIPE)
for line in item.stdout:
    print line

test.bat

@echo off
set arg1=%1
echo I wish I had %arg1% eggs!

running it:

c:\code>python sub.py
I wish I had 8 eggs!


c:\code>python sub.py
I wish I had 5 eggs!


c:\code>python sub.py
I wish I had 9 eggs!
Community
  • 1
  • 1
Keef Baker
  • 641
  • 4
  • 22
  • I get invalid syntax error (besides the missing paranthesis) – MattR Mar 21 '17 at 13:12
  • Yeah, edited. I messed up :( Basically, the shell is outside the square brackets. – Keef Baker Mar 21 '17 at 13:13
  • So this runs the same way as my code: `subprocess.call([filepath, 'arg1'])` but I cannot tell if the .bat file was run or had any impact? does not show in Python. – MattR Mar 21 '17 at 13:18
  • ok, if you want to see the output too... `item = subprocess.Popen([filepath, 'arg1'], shell=True, stdout=subprocess.PIPE)` then you can have `print item.stdout` – Keef Baker Mar 21 '17 at 13:25
  • We (mostly you) are getting close! I get `<_io.BufferedReader name=3>`. Really appreciate the help. – MattR Mar 21 '17 at 13:39
  • you might be able to loop over it with `for line in item.stdout:` then printing each line. I think it's a list even though it shows as an object. – Keef Baker Mar 21 '17 at 14:00
  • I did try that and also tried making `item` a list. The process just hangs. – MattR Mar 21 '17 at 14:03
  • If it's sat waiting for input it likely will. – Keef Baker Mar 21 '17 at 14:05
  • you might need to drop the pause. (sorry wrote about bash for a bit before i realized it was Windows) – Keef Baker Mar 21 '17 at 14:08
  • I must be a pain in you rear... I apologize. I guess if the variables are passed it *really* doesn't matter. but that would be the icing on the cake. I removed `pause` in the bat and it still hangs. Also tried `set /p Thing= %1` and also hangs. – MattR Mar 21 '17 at 14:13
  • Thank you for your patience and help! – MattR Mar 21 '17 at 14:29
0

Here is how I managed to call a variable from python to batch file. First, make a python file like this:

import os
var1 = "Hello, world!"
os.putenv("VAR1", var1) #This takes the variable from python and makes it a batch one

Second, make your batch file, by going to the folder where you want your python program to work, then right-clicking in the map, then create new text file. In this text file, write whatever you want to do with the variable and make sure you call your variable using %...% like so:

echo %VAR1%

Save this file as a batch file like so: file>save as>name_of_file.bat then select: save as file: all files.

Then to call your batch file in python, write:

os.system("name_of_file.bat")

Make sure all these files are in the same map for them to work! There you go, this worked for me, hopefully I can help some people with this comment, because I searched for so long to find how this works.

PS: I also posted on another forum, so don't be confused if you see this answer twice.

jaakdentrekhaak
  • 191
  • 2
  • 6