Python Forum
os.popen output to a list ..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.popen output to a list ..
#1
hey all good morning ....

im trying to run this command on my system that get my running process in the system
import os 

d = os.popen("""wmic process get name | findstr /v "Name 'System Idle Process' System""").read().strip()
print d
#q = ["chrome.exe" , "firefox.exe" , "opera.exe" , "iexplorer.exe" , "MicrosoftEdgeCP.exe" ] 
   
the output for the command is like this :
Quote:chrome.exe
chrome.exe
chrome.exe
chrome.exe
Microsoft.Photos.exe
svchost.exe
RuntimeBroker.exe
chrome.exe
ApplicationFrameHost.exe
WinStore.App.exe
RuntimeBroker.exe
MicrosoftEdge.exe
browser_broker.exe

i need to put them in a list then search in the list if u found any of this process ( "chrome.exe" , "firefox.exe" , "opera.exe" , "iexplorer.exe" , "MicrosoftEdgeCP.exe" ) print ("found" + "the name of process found") !!

how i can do that ?
Reply
#2
You need to split the process output string by line break. Like this:

import os

# reading the output
output = os.popen("""wmic process get name | findstr /v "Name 'System Idle Process' System""").read().strip()
# your items are separated by end of line - so we will split it
result_items = output.split("\n")
# let's do the search and print the result message
success = False
for search in ["chrome.exe", "firefox.exe", "opera.exe", "iexplorer.exe", "MicrosoftEdgeCP.exe"]:
    if search in result_items:
        success = True
        print(search + " found")
        break
if not success:
    print("Nothing found")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sum() list from SQLAlchemy output Personne 5 4,345 May-17-2022, 12:25 AM
Last Post: Personne
  How to append to list a function output? rama27 5 6,645 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  How to get program output from subprocess.Popen? glestwid 1 2,334 Aug-19-2020, 05:44 AM
Last Post: buran
  json.dumps list output qurr 12 5,073 Apr-08-2020, 10:13 PM
Last Post: micseydel
  If item in list = true, Output = xx kroh 0 1,458 Feb-19-2020, 09:17 AM
Last Post: kroh
  Difference in list output OokaydO 6 3,255 Nov-09-2019, 12:33 AM
Last Post: OokaydO
  output list reducing each time through loop 3Pinter 6 3,447 Mar-19-2019, 01:31 PM
Last Post: perfringo
  Unexpected output when searching for a string from os.popen output FujiJean 3 3,044 Oct-02-2018, 11:39 AM
Last Post: volcano63
  Converting Raw output into Nested List Nirmal 13 5,585 Aug-12-2018, 08:47 AM
Last Post: Nirmal
  25 blank lines in my sorted_fruits output list! raven61 7 4,211 Aug-09-2018, 11:30 PM
Last Post: raven61

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020