Timofey Kargin
Timofey Kargin

Reputation: 181

running shell command from python

I want to automatically check if some KB is installed on the machine. Part of my python script:

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()

What I got in the shell while executing:

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

mylog.txt:

b''

So, looks like some stupid problem with dashes or encodings. I've tried variety of commands, but nothing succeded. (Yeah, "dism" causes another tons of errors). Some advices?

Upvotes: 2

Views: 908

Answers (2)

Rakesh
Rakesh

Reputation: 82805

Try separating all of the components into element in a list.

Replace:

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

with

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

Upvotes: 2

whackamadoodle3000
whackamadoodle3000

Reputation: 6748

Here is part of your problem:

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

should be

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

Upvotes: 2

Related Questions