Passing a Python Variable to Batch File
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!
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, 2022Comments
-
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 impactimport 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 about 7 yearsI get invalid syntax error (besides the missing paranthesis)
-
Keef Baker about 7 yearsYeah, edited. I messed up :( Basically, the shell is outside the square brackets.
-
MattR about 7 yearsSo 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 about 7 yearsok, if you want to see the output too...
item = subprocess.Popen([filepath, 'arg1'], shell=True, stdout=subprocess.PIPE)
then you can haveprint item.stdout
-
MattR about 7 yearsWe (mostly you) are getting close! I get
<_io.BufferedReader name=3>
. Really appreciate the help. -
Keef Baker about 7 yearsyou 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 about 7 yearsI did try that and also tried making
item
a list. The process just hangs. -
Keef Baker about 7 yearsIf it's sat waiting for input it likely will.
-
Keef Baker about 7 yearsyou might need to drop the pause. (sorry wrote about bash for a bit before i realized it was Windows)
-
MattR about 7 yearsI 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 triedset /p Thing= %1
and also hangs. -
MattR about 7 yearsThank you for your patience and help!