Python Forum
How to Kill Dead Loop of "while"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Kill Dead Loop of "while"
#1
Hello,

I'm a very beginner of Python. I teach myself Python with videos.

My question is I write an dead loop of while. How should I stop the programme running crazily?

The instructor in the videos say,"for such a case, I should just use "ctrl + c" or go to task manager and select the programme, so I can stop the programme of "while" of loop.

However it didn't work at all with the two ways.

-ctrl +c nothing happen
-task manager: I select the programme, but I fail to kill the programme. The programme is just keeping opening mp3 one after another.

Finally I have to shut down the computer to stop the programme.

The following is the codes I write for your reference only, I don't request you to correct my codes, and other people have to point out that I need to use sleep(), and i = i +1 , etc.


import os

i = 1
while i < 8 :

 fname = 'd:/1/1' + str(i) + '.mp3'

 os.system(fname)
My OS is Windows 7
Reply
#2
If you are using,
Ubuntu - Then press "ctrl+z" should Terminate your program.
Windows - Then GOTO Task Manager, select your program and click on End Task.
Reply
#3
(Aug-22-2019, 04:40 AM)cheers100 Wrote: Hello,

I'm a very beginner of Python. I teach myself Python with videos.

My question is I write an dead loop of while. How should I stop the programme running crazily?

The instructor in the videos say,"for such a case, I should just use "ctrl + c" or go to task manager and select the programme, so I can stop the programme of "while" of loop.

However it didn't work at all with the two ways.

-ctrl +c nothing happen
-task manager: I select the programme, but I fail to kill the programme. The programme is just keeping opening mp3 one after another.

Finally I have to shut down the computer to stop the programme.

The following is the codes I write for your reference only, I don't request you to correct my codes, and other people have to point out that I need to use sleep(), and i = i +1 , etc.


import os

i = 1
while i < 8 :

 fname = 'd:/1/1' + str(i) + '.mp3'

 os.system(fname)
My OS is Windows 7


I tried your script on Windows 10 machine and am abler to stop it with Ctrl+C key combinations
Output:
The system cannot find the path specified. The system cannot find the path specified. The system cannot find the path specified. The system cannot find the path specified. The system cannot find the path specified. The system cannot find the path specified. The system cannot find the path specified. The system cannot find the path specified. Traceback (most recent call last): File "deadwhile.py", line 8, in <module> os.system(fname) KeyboardInterrupt
Reply
#4
The focus must be on the terminal window, to send CTRL+C to it.
If your mp3 player program is in front, the terminal windows won't receive CTRL+C.
In previous Python versions were a bug with catching CTRL+C on Windows.
The current version 3.7.4 does not have this problems (I use it sometimes on window).

I guess you want to start the mp3-files with the linked application.
os.system is normally used to run executable files. os.startfile (Windows only) is used to do what you are doing with os.system. Also os.system is marked as deprecated. To start executable files, the subprocess module should be used.

If you want to catch the error message (SIGINT) in very simple program, you can just use the try-except block:

def main():
    """
    Your main programm function, which calls other functions etc...
    """

try:
    main()
except KeyboardInterrupt:
    print('Got CTRL+C, quit program.')
For completeness, to avoid execution of your code, if you import it from other modules, you should add the if __name___ = '__main__' boilerplate code.


def main():
    """
    Your main programm function, which calls other functions etc...
    """

if __name__ == '__main__':
    # this is only executed, if you run this module directly with python
    # this block is not executed, if you import this module.
    try:
        main()
    except KeyboardInterrupt:
        print('Got CTRL+C, quit program.')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Aug-22-2019, 07:20 AM)DeaD_EyE Wrote: The focus must be on the terminal window, to send CTRL+C to it. If your mp3 player program is in front, the terminal windows won't receive CTRL+C. In previous Python versions were a bug with catching CTRL+C on Windows. The current version 3.7.4 does not have this problems (I use it sometimes on window). I guess you want to start the mp3-files with the linked application. os.system is normally used to run executable files. os.startfile (Windows only) is used to do what you are doing with os.system. Also os.system is marked as deprecated. To start executable files, the subprocess module should be used. If you want to catch the error message (SIGINT) in very simple program, you can just use the try-except block:
 def main(): """ Your main programm function, which calls other functions etc... """ try: main() except KeyboardInterrupt: print('Got CTRL+C, quit program.') 
For completeness, to avoid execution of your code, if you import it from other modules, you should add the if __name___ = '__main__' boilerplate code.
 def main(): """ Your main programm function, which calls other functions etc... """ if __name__ == '__main__': # this is only executed, if you run this module directly with python # this block is not executed, if you import this module. try: main() except KeyboardInterrupt: print('Got CTRL+C, quit program.') 

Thank you very much for your help. My Python version is the latest one - 3.7.4

I just learnt Python for a few days. So, it is difficult for me to understand those codes you wrote for me.

But I understood what you meant about "focus", and why ctrl +c doesn't work now.

I will have to limp away currently.

And I appreciate the other two persons who kindly responded to me too.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  os.kill elstolbo 1 2,331 Jan-26-2020, 04:13 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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