Python基礎 呼叫shell和cmd命令

2021-08-10 23:40:46 字數 1071 閱讀 2967

啟動

catcmd

= "adb -s "

+ tvip +

":5555 logcat -v time > "

+ logpath

printcatcmd

self

.lcprocess = subprocess.popen(catcmd

, stdout

=subprocess.pipe

, stderr

=subprocess.pipe

, shell

=true

)

關閉:

if(

self

.lcprocess <>

none

):   

self

.lcprocess.kill()

self

.lcprocess =

none

上述方法,將無法關閉,因為啟動cmd來啟動adb  logcat,關閉時,只關閉了cmd程序,但是adblogcat被系統託管,無法關閉。

解決方案:

啟動

logcat_file

= open

(logpath

, 'w'

)logcmd =

"adb -s "

+ tvip +

":5555 logcat -v time"

self

.lcprocess = subprocess.popen(logcmd

, stdout

=logcat_file

, stderr

=subprocess.pipe)

關閉:

if(

self

.lcprocess <>

none

):   

self

.lcprocess.terminate()

在執行命令時,在linux平台,shell=true,否則會報出找不到檔案的錯誤。

Python 呼叫shell指令碼

python呼叫shell指令碼,有兩種方法 os.system cmd 或os.popen cmd 前者返回值是指令碼的退出狀態碼,後者的返回值是指令碼執行過程中的輸出內容。實際使用時視需求情況而選擇。現假定有乙個shell指令碼test.sh bin bash 1.echo hello worl...

python呼叫shell命令

在python程式中呼叫shell命令 此函式會啟動子程序,在子程序中執行command,並返回command命令執行完畢後的退出狀態,如果command有執行內容,會在標準輸出顯示。這實際上是使用c標準庫函式system 實現的。缺點 這個函式在執行command命令時需要重新開啟乙個終端,並且無...

python 呼叫shell命令

python中的commands模組用於呼叫shell命令,有3中方法 commands.getstatus 返回執行狀態 commands.getoutput 返回執行結果 commands.getstatusoutput 返回乙個元組,執行狀態和執行結果 其他執行shell命令的方法還有 1.o...