def enum(key): with open(key) as handle: index = 0 while True: try: yield Key(*winreg.EnumValue(handle, index)) index += 1 except WindowsError: break