Python Forum
Testing If a string has anything in it
Thread Rating:
  • 4 Vote(s) - 3.25 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Testing If a string has anything in it
#1
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()
Reply
#2
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())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  testing if a character in a string is an intended one Skaperen 2 2,661 Feb-27-2018, 02:51 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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