Python Script not exiting with keyboard Interrupt
Question:
I made a simple script to take screenshots and save it to a file every few seconds.
Here is the script :
from PIL import ImageGrab as ig
import time
from datetime import datetime
try :
while (1) :
tm = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
try :
im = ig.grab()
im.save('/home/user/tmp/' + tm + '.png')
time.sleep(40)
except :
pass
except KeyboardInterrupt:
print("Process interrupted")
try :
exit(0)
except SystemExit:
os._exit(0)
It works perfectly (in Ubuntu 18.04, python3), but the keyboard interrupt does not work. I followed this question and added the except KeyboardInterrupt:
statement. It again takes a screenshot when I press CTRL+C
. Can someone help with this?
Answers:
You need to move your keyboard interrupt exception handling one up. The keyboard interrupt never reaches your outer try/except block.
You want to escape the while
loop; exceptions inside the while
block are handled here:
while True:
tm = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
try :
im = ig.grab()
im.save('/home/user/tmp/' + tm + '.png')
time.sleep(40)
except : # catch keyboard interrupts and break from loop
pass
If you break from the loop on keyboard interrupt, you leave the while
loop and it won’t grab again.
Use the following code to fix your problem:
from PIL import ImageGrab as ig
import time
from datetime import datetime
while (1):
tm = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
try:
im = ig.grab()
im.save('/home/user/tmp/' + tm + '.png')
time.sleep(40)
except KeyboardInterrupt: # Breaking here so the program can end
break
except:
pass
print("Process interrupted")
try:
exit(0)
except SystemExit:
os._exit(0)
I made a simple script to take screenshots and save it to a file every few seconds.
Here is the script :
from PIL import ImageGrab as ig
import time
from datetime import datetime
try :
while (1) :
tm = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
try :
im = ig.grab()
im.save('/home/user/tmp/' + tm + '.png')
time.sleep(40)
except :
pass
except KeyboardInterrupt:
print("Process interrupted")
try :
exit(0)
except SystemExit:
os._exit(0)
It works perfectly (in Ubuntu 18.04, python3), but the keyboard interrupt does not work. I followed this question and added the except KeyboardInterrupt:
statement. It again takes a screenshot when I press CTRL+C
. Can someone help with this?
You need to move your keyboard interrupt exception handling one up. The keyboard interrupt never reaches your outer try/except block.
You want to escape the while
loop; exceptions inside the while
block are handled here:
while True: tm = datetime.today().strftime('%Y-%m-%d-%H:%M:%S') try : im = ig.grab() im.save('/home/user/tmp/' + tm + '.png') time.sleep(40) except : # catch keyboard interrupts and break from loop pass
If you break from the loop on keyboard interrupt, you leave the while
loop and it won’t grab again.
Use the following code to fix your problem:
from PIL import ImageGrab as ig
import time
from datetime import datetime
while (1):
tm = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
try:
im = ig.grab()
im.save('/home/user/tmp/' + tm + '.png')
time.sleep(40)
except KeyboardInterrupt: # Breaking here so the program can end
break
except:
pass
print("Process interrupted")
try:
exit(0)
except SystemExit:
os._exit(0)