簡體   English   中英

Python將數據發送到作為終端參數傳遞的可執行文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM