当前位置: 动力学知识库 > 问答 > 编程问答 >

command line - 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 to the executable.

1) is this even possible?

2) if so, how can I do this in Python

网友答案:

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]
分享给朋友:
您可能感兴趣的文章:
随机阅读: