Python Forum

Full Version: Testing If a string has anything in it
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Guys,

In the code below I am going into the registry to look for a certain item. In this case I am using (RtkAudioService). Once it finds it will give the location of it. ("C:\Program Files\Realtek\Audio\HDA\RtkAudioService64.exe") Now when someone enters something else in the input that is not located in this location I want it to come up and state something that this is not located here or something along that line. All I am getting is the following Error bellow. How would you guys approach this?

What Program would you like to search for? f
Error:
Traceback (most recent call last): File "C:\Python27\SearchPath.py", line 29, in <module> print getMDACversion() File "C:\Python27\SearchPath.py", line 15, in getMDACversion hKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, RegLocation) WindowsError: [Error 2] The system cannot find the file specified
import _winreg
from _winreg import *

usr_input = raw_input('What Program would you like to search for? ')
location = 'SYSTEM\\ControlSet001\\services\\'
RegLocation = location + usr_input

def getMDACversion():
hKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, RegLocation)
result = _winreg.QueryValueEx(hKey, "ImagePath")
return result[0]

print getMDACversion()
Rather than try to fix all of the errors, here's one that works.
This was done on python 3.6.2 so you may have to tweak a bit
please note that you shouldn't use _winreg, rather winreg
import winreg
import traceback
import logging
import sys

def getMDACversion():
    base = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
    try:
        hkey = int(winreg.OpenKey(
            base, 'SOFTWARE\\Python\\PythonCore', 0, winreg.KEY_READ))
        print(hkey)
        # result = winreg.QueryValueEx(hKey, "ImagePath")
        # return result[0]
    except:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        logging.error(repr(traceback.format_exception(exc_type,
                    exc_value, exc_traceback)))

print(getMDACversion())