Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop Condition Question
#1
I want to iterate over a loop until an OSError is thrown. That's it. What's the best way to do that. What I'm doing is using winreg.EnumKey to make a list of all sub-keys of a key. The function returns one key at a time. So, I need to loop it, increasing the index each time to enum all keys. How do I stop the loop at an OSError (which is what the docs recommend)?
Just to show some code (this is not how I want to do it)
i = 0
while i < 5:
    list = winreg.EnumKey(<key>, i)
    i += 1
I want something like
while not OSError:
    ....
But my limited understanding is causing errors.

Help is, as always, much appreciated.
malonn
Reply
#2
I am assuming you have tried:

i = 0
while not OSError:
  list = winreg.EnumKey(<key>, i)
  i += 1
what error did you get?
Reply
#3
I'm not sure the error, but it throws one at the while condition in the following code:
reg_key = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList\\Profiles'

if ctypes.windll.shell32.IsUserAnAdmin():
    with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_key, 0, winreg.KEY_ALL_ACCESS) as k:
        ns = winreg.QueryInfoKey(k)[0]
        if ns > 1:
            i = 0
            while not OSError:
                subs = winreg.EnumKey(k, i)
                i += 1
            print(subs)
    input('Check your Regedit.')
else:
    ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, 'run_as.py', None, 1)
I checked by placing an input() call right after
while not OSError:
line and the screen does not pause for user input. So, it errors out at that line, I just can't figure out how to trap the error.
Reply
#4
If you are expecting the OSError from the winreg call:

i = 0
while i < 5:
    try:
        list = winreg.EnumKey(<key>, i)
    except OSError:
        break
    i += 1
Although, that's really a for loop:

for i in range(5):
    try:
        list = winreg.EnumKey(<key>, i)
    except OSError:
        break
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Hey, thanks @Vysero and @ichabod801! I was thinking I may need a try:except: thingy (still learning), but wasn't sure. It works great though @ichabod801; thanks.

Problem solved.
Reply
#6
I did not realize you were trying to throw the error. I thought you were trying to loop off of the catch.
Reply
#7
Yeah, NP. The function errors when there is no more sub-keys within a key.

Time to post my next sticking point in a new thread...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to run this loop but it skips all the condition vensilver 5 1,900 Feb-17-2022, 05:38 PM
Last Post: deanhystad
  A question about 'Event loop is closed' fc5igm 2 2,201 Oct-05-2021, 02:00 AM
Last Post: fc5igm
Exclamation question about input, while loop, then print jamie_01 5 2,649 Sep-30-2021, 12:46 PM
Last Post: Underscore
  for loop question KEYS 1 1,722 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  Netmiko Loop question sc00ter 2 3,311 Oct-24-2020, 10:54 PM
Last Post: sc00ter
  while loop question KEYS 2 2,012 Sep-26-2020, 11:02 PM
Last Post: KEYS
  New to programming, loop question tomyan 1 1,633 Sep-25-2020, 04:32 PM
Last Post: Larz60+
  while loop question spalisetty06 2 1,846 Aug-13-2020, 04:18 PM
Last Post: buran
  question about for loop Than999 5 2,468 Jun-09-2020, 02:16 PM
Last Post: Emekadavid
  else condition not called when if condition is false Sandz1286 10 5,855 Jun-05-2020, 05:01 PM
Last Post: ebolisa

Forum Jump:

User Panel Messages

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