簡體   English   中英

從python運行shell命令

[英]running shell command from python

我想自動檢查計算機上是否安裝了某些KB。 我的python腳本的一部分:

import subprocess
result = subprocess.run(['cmd', '/c wmic qfe'], shell=True, stdout=subprocess.PIPE)
ftemp = open('mylog.txt','w') #just to check what is going wrong
ftemp.write(str(result.stdout))
ftemp.close()
if str(result.stdout).find('KB2999226')==-1:
    print('Nah, you don't have KB')
    sys.exit()

執行時我在shell中得到了什么:

qfe" - Alias not found.
Nah, you don't have KB

mylog.txt:指定

b''

因此,看起來像是一些破折號或編碼的愚蠢問題。 我嘗試了各種命令,但沒有成功。 (是的,“ dism”會引起大量錯誤)。 有什么建議嗎?

這是您的問題的一部分:

print('Nah, you don't have KB')

應該

print("Nah, you don't have KB")

嘗試將所有組件分成列表中的元素。

更換:

result = subprocess.run(['cmd', '/c wmic qfe'], shell=True, stdout=subprocess.PIPE)

result = subprocess.run(['cmd', '/c', 'wmic', 'qfe'], shell=True, stdout=subprocess.PIPE)

暫無
暫無

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

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