Python Forum
How to prevent Command Promt from closing when I turn it off in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to prevent Command Promt from closing when I turn it off in Python?
#1
I am very new to python. I have a little program and I want it run in the following order :

- First I set a method
time.sleep(10)
within code to suspends execution for the 10 seconds in my test file.
- I run my test file by command prompt.
- While the program is performing a suspension of 10 seconds, suddenly I click the X close button in the top-right corner of the window, the program will not immediately shut down but it will run command
print('Close terminal event detected.')
and delay about 1 second by
time.sleep(1)
before it shuts off.

I wrote the code below but I'm still stuck. Would someone please tell me how to do it? Thank you.
import time
import os

time.sleep(10)

# If the terminal is closed while time.sleep(10) is running, the program will print the following line and run time.sleep(1) before exiting
print('Close terminal event detected.')

time.sleep(1)
Reply


Messages In This Thread
How to prevent Command Promt from closing when I turn it off in Python? - by binhduonggttn - Mar-06-2020, 07:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 795 Aug-09-2023, 05:51 PM
Last Post: Calab
Smile How we can prevent screen recording murad_ali 3 1,880 Jul-29-2022, 10:29 AM
Last Post: DeaD_EyE
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,563 Jun-27-2022, 08:45 AM
Last Post: Tmagpy
  How to prevent python from going to new line in for loop? idknuttin 3 4,998 Feb-11-2022, 05:40 AM
Last Post: deanhystad
  my python file closing rwahdan 1 1,476 Jun-30-2021, 08:25 AM
Last Post: Gribouillis
  Reading data to python: turn into list or dataframe hhchenfx 2 5,442 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  Prevent urllib.request from using my local proxy spacedog 0 2,927 Apr-24-2021, 08:55 PM
Last Post: spacedog
  "Automate the Boring Stuff with Python" creating a path works but only for CMD promt Milos 2 2,912 Nov-28-2020, 01:08 PM
Last Post: Larz60+
  Turn coordinates in string into a tuple Kolterdyx 3 2,813 Jun-10-2020, 05:04 AM
Last Post: buran
  Prevent a PDF from being edited using Python? PythonNPC 1 2,339 May-05-2020, 09:04 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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