I write a spider and save those alreadly caught data in a list, I want to save data when I press the ctrl+c, but I find it that while handling the KeyboardInterrupt, UnicodeDecodeError happens in "save_flag = input('If continue program enter "y", save and exit enter "n": ')", the traceback is at below,
Traceback (most recent call last):
File "D:\PyProject\win32\fileshutil.py", line 10, in <module>
time.sleep(1)
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\PyProject\win32\fileshutil.py", line 13, in <module>
save_flag = input('If continue program enter "y", save and exit enter "n": ')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
import sys
import time
try:
while True:
print('1')
"""
some code
"""
time.sleep(1)
except KeyboardInterrupt:
save_flag = input('If continue program enter "y", save and exit enter "n": ')
if save_flag == 'y':
"""
save code
"""
sys.exit()
elif save_flag == 'n':
sys.exit()
I find it that the text in the input is print correctly, and I do not know why is happens. I try to def the code in handle KeyboardInterrput in a function like below,
def handle_keyboard_interrupt():
save_flag = input('If continue program enter "y", save and exit enter "n": ')
if save_flag == 'y':
"""
save code
"""
sys.exit()
elif save_flag == 'n':
sys.exit()
but same UnicodeDecodeError happens. and I tried use
'If continue program enter "y", save and exit enter "n": '.encode(encoding="utg-8")
to force encoding with utf-8, but same error.