ECC ECC - 8 months ago 80
Python Question

Check if a Windows Service exists with Python

I wrote a program in python that interacts with a Windows service. However, before doing it, I need check if the service is installed.

I'm using the following code to get the service. But if it not exists, I receive error.


This is the error:

Traceback (most recent call last):
File "win32serviceutil.pyc", line 835, in SvcRun
File "", line 54, in SvcDoRun
File "", line 103, in main
File "", line 57, in start_service
File "main.pyc", line 495, in QueryServiceStatus
File "main.pyc", line 80, in SmartOpenService
error: (1060, 'GetServiceKeyName', 'The specified service does not exist as an installed service')

Is there any way I can check it?

Answer Source

It seems like a Try-Except block would be the easiest solution:

    print "Windows service NOT installed"
    print "Windows service installed"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download