Python Forum

Full Version: Loop not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
while 1:

    import webbrowser


    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

    webbrowser.get(chrome_path).open('https://www.halowaypoint.com/en-us/news')


    from time import sleep
    import os

    sleep(30)
     
    os.system("taskkill /im chrome.exe /f")
Idk why it wont keep looping, any help?
sleep(30) will make it wait 30 seconds
I know, but it still wont loop?
How are you determining that the loop isn't running?

Also, you don't need to import on every iteration; those can just be at the top of your file.
It doesn't kill chrome, and start again...
Sounds like "it doesn't kill chrome" is the problem, not the loop. Or maybe it does kill chrome and you just don't see that. What happens if you add a delay after killing chrome and maybe sprinkle a few print statements here and there to monitor progress.
No i see it. Shouldn't it just work?
After it opens up the website it waits for the chrome process to end, then it does the wait. This was immediately obvious when I modified the program like this:
import webbrowser
from time import sleep
import os

while 1:
    print('start chrome')
    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
    print('going to website')
    webbrowser.get(chrome_path).open('https://www.halowaypoint.com/en-us/news')
    print('sleep')
    sleep(10)
    print('kill chrome')
    os.system("taskkill /im chrome.exe /f")
    print('Wait again')
    sleep(10)
    print('repeat')
It would be nice if programs worked, but people are stupid and make mistakes. That is why debugging is just as important as coding. I was stupid and could not spot why the program didn't work just by looking, but a few minutes spent adding some debug code exposed the problem right away.
Also try not to use os.system() anymore.
subprocess replace it.
import subprocess

subprocess.run("taskkill /im chrome.exe /f")

# Or the safer way if think of shell injection vulnerabilities
#subprocess.run(['taskkill', '/im', 'chrome.exe', '/f'], shell=False)