Passing a Python Variable to Batch File

12,735

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!
Share:
12,735
MattR
Author by

MattR

I am a Data Analyst who uses Python to solve various data-centered problems. I also create powerful programs using Python. I use SQL daily along side SSIS and Tableau

Updated on December 02, 2022

Comments

  • MattR
    MattR over 1 year

    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?

  • MattR
    MattR about 7 years
    I get invalid syntax error (besides the missing paranthesis)
  • Keef Baker
    Keef Baker about 7 years
    Yeah, edited. I messed up :( Basically, the shell is outside the square brackets.
  • MattR
    MattR about 7 years
    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.
  • Keef Baker
    Keef Baker about 7 years
    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
  • MattR
    MattR about 7 years
    We (mostly you) are getting close! I get <_io.BufferedReader name=3>. Really appreciate the help.
  • Keef Baker
    Keef Baker about 7 years
    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.
  • MattR
    MattR about 7 years
    I did try that and also tried making item a list. The process just hangs.
  • Keef Baker
    Keef Baker about 7 years
    If it's sat waiting for input it likely will.
  • Keef Baker
    Keef Baker about 7 years
    you might need to drop the pause. (sorry wrote about bash for a bit before i realized it was Windows)
  • MattR
    MattR about 7 years
    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
    MattR about 7 years
    Thank you for your patience and help!