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