[英]Python send data to an executable passed as parameter in terminal
我有一个python脚本,它从命令行,可执行文件和文件中获取2个参数。 在我做一些计算之后,我需要将stdin的计算结果传递给可执行文件。
1)这甚至可能吗? 2)如果是这样,我怎么能用Python做到这一点
首先,你永远不应该使用os.system,这是一个非常危险和坏习惯。
至于您的问题,使用子进程可以执行以下操作:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.