我正在Linux中使用python開發一個小工具。之前我使用的是Python 2.7,但現在我將其更改爲Python 3.4以查看它是否可以幫助解決我的問題。當我給下面的代碼:無法在子流程命令中使用shell = True時運行命令
try:
x=subprocess.check_output(command, shell=True, timeout=3)
except subprocess.TimeoutExpired as exc:
print ("Timeout bro")
exit()
except Exception as e:
msg = "Some issues in fetching details"
print (msg)
由於命令從其他設備獲取詳細信息和設備工作不正常,這是越來越後3秒超時和打印消息「超時兄弟」。我使用shell = True讀取了安全問題,因此我將它作爲shell = False一次,第二次刪除了該參數。
try:
x=subprocess.check_output(command, shell=False, timeout=3)
except subprocess.TimeoutExpired as exc:
print ("Timeout bro")
exit()
except Exception as e:
msg = "Some issues in fetching details"
print (msg)
我在不同的地方看過,這個命令和shell = False一樣工作得很好。但是,只要我用shell=False
運行上面的代碼,代碼直接打印「取得細節時的一些問題」,而不用等待3秒。有沒有什麼辦法可以在沒有shell = True的情況下運行代碼?請幫忙。謝謝!
命令是什麼? –
另外shell = False是默認的,所以你不需要傳遞它。 –