結論
except KeyboardInterrupt
と、「control + c」を使う
背景
ボットなどを走らせている時、sleepではなく任意の好きなタイミングでプログラムを一時停止したい。
この際、sleepを使うと任意のタイミングでできないし、ctrl + c
ではプログラムが終了してしまう。
サンプルコード
import time n = 0 while True: try: time.sleep(1) n += 1 print(n) except KeyboardInterrupt: input('Program paused. Hit ENTER to resume')