[英]Using Python to run a file with the terminal
我正在尋找一種通過終端運行文件的方法,但我不想輸入它,而是想運行一個 python 腳本,該腳本為我通過終端運行文件。
此代碼應通過終端運行:
cd LCD-show
sudo ./LCD35-show
我已經嘗試過這個並在 Python 中運行它:
import os
os.system("./LCD35-show")
但這不起作用。 我怎么解決這個問題?
試穿這個尺寸:
import subprocess
p1 = subprocess.run("cd LCD-show ; sudo ./LCD35-show", shell=True, text=True, capture_output=True)
print(p1.returncode)
print(p1.stdout)
print(p1.stderr)
shell=True
選項通過 shell 運行整個 arg,因此用分號分隔的多個命令起作用。 text=True
意味着將stdout
和stderr
視為字符串,而不是字節。 最后, capture_output=True
意味着實際捕獲stdout
和stderr
作為屬性。
如果你在myFile.sh
中有一個完整的命令文件,那基本上是一樣的。 請注意myFile.sh
的 exec 權限和任何PATH
考慮事項; 你可能需要做./myFile.sh
:
p1 = subprocess.run("myFile.sh", shell=True, text=True, capture_output=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.