By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

Process id error

arunhero99
P: 3
Hi Guys,

Given below is the function i have defined to find process id of the running process xyz.exe. But when i try to run this function when the process xyz.exe is not running, obviously "pid = p[0].Properties_('ProcessId').Value" shows error as the process doesn't exist.
but i want it to return pid="" ,to use that value to check if the process is running or not.I am now managing with a try block to catch the exception and return pid="".but this looks bit awkward.
So would like some experts to pitch in with some idea to implement something more elegant.

Expand|Select|Wrap|Line Numbers
  1. from win32com.client import GetObject
  2. WMI = GetObject('winmgmts:')
  3.  
  4. def getPid():
  5.     pid = ""
  6.                 p=None
  7.     processes = WMI.InstancesOf('Win32_Process')
  8.     p = WMI.ExecQuery('select * from Win32_Process where Name="xyz.exe"')
  9.     pid = p[0].Properties_('ProcessId').Value
  10.     return pid
Any ideas?
Thanks,
aRuN
Jun 1 '07 #1
Share this Question
Share on Google+
5 Replies


bartonc
Expert 5K+
P: 6,596
Hi Guys,

Given below is the function i have defined to find process id of the running process xyz.exe. But when i try to run this function when the process xyz.exe is not running, obviously "pid = p[0].Properties_('ProcessId').Value" shows error as the process doesn't exist.
but i want it to return pid="" ,to use that value to check if the process is running or not.I am now managing with a try block to catch the exception and return pid="".but this looks bit awkward.
So would like some experts to pitch in with some idea to implement something more elegant.

Expand|Select|Wrap|Line Numbers
  1. from win32com.client import GetObject
  2. WMI = GetObject('winmgmts:')
  3.  
  4. def getPid():
  5.     pid = ""
  6.                 p=None
  7.     processes = WMI.InstancesOf('Win32_Process')
  8.     p = WMI.ExecQuery('select * from Win32_Process where Name="xyz.exe"')
  9.     pid = p[0].Properties_('ProcessId').Value
  10.     return pid
Any ideas?
Thanks,
aRuN
I'd probably go with:
Expand|Select|Wrap|Line Numbers
  1. #....
  2.     try:
  3.         pid = p[0].Properties_('ProcessId').Value
  4.     except IndexError: # or what ever error you are getting
  5.         pid = None
Jun 1 '07 #2

arunhero99
P: 3
Thanks for the reply...
i am actually using try and a generic except to implement this.
I just wanted to know if there is any other function in python which doesnt touch win32api to find process id.

thanks again.
Cheers...
Jun 1 '07 #3

Expert 100+
P: 511
just for information sharing, i use Tim golden's WMI module
Expand|Select|Wrap|Line Numbers
  1. import wmi
  2. c = wmi.WMI ()
  3. for process in c.Win32_Process ():
  4.   print process.ProcessId, process.Name
  5.   if process.Name=="firefox.exe":
  6.         print "do something"
  7.  
Jun 1 '07 #4

bartonc
Expert 5K+
P: 6,596
Thanks for the reply...
i am actually using try and a generic except to implement this.
I just wanted to know if there is any other function in python which doesnt touch win32api to find process id.

thanks again.
Cheers...
We actually call
Expand|Select|Wrap|Line Numbers
  1. ...
  2.     except:
  3.         # do something
a naked except and consider to be too broad. For example, if the error is a memory error and you go off and do something that needs more memory, you have not helped the situation.
Just a thought...
Jun 1 '07 #5

Expert 100+
P: 511
Thanks for the reply...
i am actually using try and a generic except to implement this.
you can use :
Expand|Select|Wrap|Line Numbers
  1. try: ...
  2. except Exception,err:
  3.    print err...
  4.  
I just wanted to know if there is any other function in python which doesnt touch win32api to find process id.

thanks again.
Cheers...
no and yes.
no means not as far as i know. yes means you can use system() or os.popen() or subprocess module to call external commands like tasklist (XP) , pslist (M$, originally sysinternals) or others to display the processes for you. Then parse the results in Python.
Jun 1 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Browse more Python Questions on Bytes