Skip to content Skip to sidebar Skip to footer

Python Send Data To An Executable Passed As Parameter In Terminal

I have a python script which gets 2 parameters from the command line, an executable and a file. After I do some computation I need to pass by stdin the result of this computation t

Solution 1:

First, you should never use os.system that's a very dangerous and bad habit.

As for your problem, using subprocess you can do the following:

from subprocess import Popen, PIPE, STDOUT

#do some stuff 
data = do_some_computation_from_file

#prepare your executable using subprocess.Popen
exe = Popen(['your_executable'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)

#pass in the computed data to the executable and grap the result
result = exe.communicate(input=data)[0]

Post a Comment for "Python Send Data To An Executable Passed As Parameter In Terminal"