John
John

Reputation: 341

Pass a Python input parameter to a Batch File

Is it at all possible to build a Python GUI (lets say using Tkinter) and then pass the users input from the Python GUI into a windows batch file. My objective is to make batch files have a nice front end using Python.

Simple example:

In the Python code the user will be asked for a date

date = inputInt("Please enter Date yyyymmdd")  

Now I need to put this date value into a windows batchfile.

Upvotes: 3

Views: 1430

Answers (2)

John
John

Reputation: 341

I used the following code to send the data to a text file

import sys

startdate = input("Please Enter StartDate YYYYMMDD ")
orig_stdout = sys.stdout
f = open('startdate.txt', 'w')
sys.stdout = f
print(startdate)
sys.stdout = orig_stdout
f.close()

I then used the following in my batch file to read the text file contents

@echo off
set /p startdate=<startdate.txt
echo %startdate%

Upvotes: 0

AndrejH
AndrejH

Reputation: 2119

When running the the Python program you should use a pipe, to redirect it's stdout to stdin of the batch file. In the batch file you can just wait on the stdin until something is outputed by the Python program. Take a look here to see how to read an input stream in batch. It would look something like this:

python myprogram.py | batch_file.bat

Upvotes: 2

Related Questions