Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop not working
#1
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?
Reply
#2
sleep(30) will make it wait 30 seconds
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I know, but it still wont loop?
Reply
#4
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.
Reply
#5
It doesn't kill chrome, and start again...
Reply
#6
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.
Reply
#7
No i see it. Shouldn't it just work?
Reply
#8
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.
Reply
#9
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  while loop not working-I am using sublime text editor mma_python 4 1,060 Feb-05-2023, 06:26 PM
Last Post: deanhystad
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Why is the while loop not working? mcoliver88 5 3,044 Aug-18-2020, 03:27 PM
Last Post: deanhystad
  Infinite loop not working pmp2 2 1,608 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  for loop script over telnet in Python 3.5 is not working abhijithd123 1 2,860 May-10-2020, 03:22 AM
Last Post: bowlofred
  How to keep a loop containing a web import working in python executable? coder1384 3 2,819 Feb-22-2020, 06:49 PM
Last Post: snippsat
  For loop not working pjgrah01 4 2,755 Nov-03-2019, 11:58 PM
Last Post: pjgrah01
  Appending to list not working and causing a infinite loop eiger23 8 3,941 Oct-10-2019, 03:41 PM
Last Post: eiger23
  Working on my FOR loop wilsonrivas 1 2,169 May-24-2019, 04:05 PM
Last Post: heiner55
  plotting inside the loop is not working jenya56 4 3,033 Apr-10-2019, 08:11 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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