realp
realp

Reputation: 627

Opening an .exe and passing commands to it via Python subprocess?

So I have my .exe program that opens but i want to pass strings to it from my python script. Im opening the exe like this

import subprocess
p = subprocess.Popen("E:\Work\my.exe", shell=True)
#let user fill in some tables
p.communicate("userInfo")

I want to pass a string to this program while having it just run in the background and not take over any ideas?

Upvotes: 2

Views: 1838

Answers (1)

MortalFool
MortalFool

Reputation: 1111

From the Python documentation for Using the subprocess module:

You don’t need shell=True to run a batch file, nor to run a console-based executable.

From the Python documentation for Popen Objects :

Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

Code example:

from subprocess import Popen, PIPE

p = Popen(r"E:\Work\my.exe", stdin=PIPE)
p.communicate("userInfo")

Upvotes: 2

Related Questions