![](/img/trans.png)
[英]How can I get the output of a Python subprocess command that contains a pipe?
[英]how can I pipe result of which command to another command in subprocess?
我如何將 pipe 結果中的which somecommand
復制到outerCommand
中,以便復制我將在 shell 中執行的結果作為outercommand 'which somecommand'
根據文檔的建議,我會避免使用低級接口Popen
,從長遠來看很難正確使用,並且盲目地使用shell=True
,這可能會使您暴露於shell injection attacks 。
因此,如果這就是您所需要的,我會建議一種更簡單的方法:只需捕獲 output 並使用新的 subprocess.run subprocess.run()
API 的新capture_output
參數使用它。
這將得到你需要的:
completed_process = subprocess.run(['outercommand', subprocess.run(['which', 'somecommand'], capture_output=True).stdout], capture_output=True)
completed_process.check_returncode()
print(completed_process.stdout.decode('utf-8'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.