1

I am trying to get an output from a .bat file in a python script, code works fine if I hard code a variable value in .bat file, but I want that value to be dynamic. This is code I am using to execute the external file.

command = 'C:/this/this.bat'
p = subprocess.Popen(command, universal_newlines=True,
shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

text = p.stdout.read()
retcode = p.wait()

file this.bat requires a user input but I am not sure how to provide it inside python script, e.g. from a variable. Thanks for the help.

1 Answer 1

1

I've dealt with something similar. The way I solved it was to prompt the user for values in python. Then using subprocess pass those values into the .bat file.

command = [shutil.which('C:/this/this.bat') ,sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]]
subprocess.Popen(command).wait()

This assumes you can change the .bat file to take parameters instead of prompts.

Sign up to request clarification or add additional context in comments.

1 Comment

Answers this, Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.