簡體   English   中英

使用shebang使用subprocess.call執行python腳本

[英]Executing python scripts with subprocess.call using shebang

我正在用Python 3編寫一個(某種程度上)模塊化的應用程序,我想從它運行任意程序,表示程序是在運行時指定的,而不一定是python腳本。

所以我用例如,

subprocess.call([spam, "-i", eggs, "-o", ham])

如果spam是一個python腳本,shebang到python3和可執行權限,我得到

OSError: [Errno 8] Exec format error

如果我

subprocess.call(["python3", spam, "-i", eggs, "-o", ham])

它工作正常。

你知道為什么嗎? 如何在不指定python3情況下運行spam

您需要使用shell=True ,並且需要將您的數組轉換為命令字符串,如下所示:

subprocess.call(' '.join([spam, "-i", eggs, "-o", ham]), shell=True)

這將調用shell而不是直接命令,shell應該能夠處理shebang。

嘗試

subprocess.call(['spam.py', "-i", eggs, "-o", ham])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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