我有一個可執行文件,我需要通過一個python腳本來運行和可執行文件需要一個文件路徑作爲參數,就像這樣:有沒有辦法使用字符串中的數據代替子進程中的文件路徑參數?
./myExecutable /pathToFileToProcess
我希望我能和POPEN運行合格可執行我表示我的「/ pathToFileToProcess」數據的字符串,而不必將其寫入磁盤。
我相信我試圖做的是非常相似,在這裏問的問題: Python string as file argument to subprocess
但是建議的解決方案是不是爲我工作。下面是我試圖實現它:
from subprocess import Popen, STDOUT, PIPE
stringDataFromFile = "the data to be processed"
p = Popen(['./myExecutable'], shell = False, stdout = PIPE, stderr = PIPE, stdin = PIPE)
stdout, stderr = p.communicate(input = stringDataFromFile)
print(stdout)
print(stderr)
當我運行此我得到在標準輸出爲./myExecutable的一般用法消息是,如果你不提供文件來處理髮生了什麼。注意:我需要捕獲標準輸出,所以我已經實現它與建議略有不同。
我在想,差異必須是./myExecutable不會接受它通過stdin輸入,其中鏈接示例中的程序會。或者,我只是做錯了。
有關如何做到這一點的任何想法,或者甚至可能嗎?
'stdin = stdout'? –