Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have the following code. It runs fine, but I need it to post the results to a

ID: 3885514 • Letter: I

Question

I have the following code. It runs fine, but I need it to post the results to a text file and it's not. It needs to just post every service name and state that it's running, like below:

Acronis Scheduler2 Service Running
Acronis Nonstop Backup Service Running
Application Information Running
ASUS HID Access Service Running

Code:

import subprocess

cmd = 'wmic process where "Status=Running get Name,processid,Status'

proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

filename ="running_process.txt"

writeToFile = open(filename,'w')

#sc query command in power shell display all the running servces

#It display information about active service only

#try typing sc query in cmd

all_running_process = subprocess.check_output(['sc','query'])

#all_running_process contain multiple line for a service

#first line is service name and 4th line is its status

#print all_running_process

#split all lines and store into array

running_process_array = all_running_process.decode().split(" ")

#since service name is on the line number: 1, 11, 21,..

#so need to write i+1

#since status is on line 4, 14, 24 ....

#so need to write i+4

#and increment i by 10 since service repeates after every 10 line

i=0

while i+4<len(running_process_array):

print (writeToFile, running_process_array[i+1])

print (writeToFile, running_process_array[i+4])

i=i+10

Explanation / Answer

To write to a file you have to call the file object.write() method.

script.py

#

# Start Here

#

import subprocess

cmd = 'wmic process where "Status=Running get Name,processid,Status'

proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

filename ="running_process.txt"

writeToFile = open(filename,'w')

#sc query command in power shell display all the running servces

#It display information about active service only

#try typing sc query in cmd

all_running_process = subprocess.check_output(['sc','query'])

#all_running_process contain multiple line for a service

#first line is service name and 4th line is its status

#print all_running_process

#split all lines and store into array

running_process_array = all_running_process.decode().split(" ")

#since service name is on the line number: 1, 11, 21,..

#so need to write i+1

#since status is on line 4, 14, 24 ....

#so need to write i+4

#and increment i by 10 since service repeates after every 10 line

i=0

while i+4<len(running_process_array):

writeToFile.write(running_process_array[i+1])

writeToFile.write(running_process_array[i+4]+" ")

i=i+10

PLEASE RATE!

Thanks !