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?

0

2 Answers 2

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!
Sign up to request clarification or add additional context in comments.

11 Comments

I get invalid syntax error (besides the missing paranthesis)
Yeah, edited. I messed up :( Basically, the shell is outside the square brackets.
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.
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
We (mostly you) are getting close! I get <_io.BufferedReader name=3>. Really appreciate the help.
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.