【问题标题】:How do I prevent ^C from showing when displayed when pressing CTRL+C按下 CTRL+C 时如何防止 ^C 显示
【发布时间】:2019-06-16 07:18:40
【问题描述】:

我试图阻止^C 在我的脚本运行时用户按下CTRL+C 时显示。

我为什么要防止这种情况发生?

因为这样的事情会发生,而且看起来不太好:

$ python3 myscript.py
^CYour pressed CTRL+C

我知道有一个类似的问题here,但它在 Python 中不起作用

【问题讨论】:

标签: python python-3.x terminal


【解决方案1】:

我找到了一个简单的方法!

# Returning the cursor to home and dont create a new line
print("\r", end="")

# Now we are able to print on the line where ^C would be displayed
print("Your pressed CTRL+C")

【讨论】:

    【解决方案2】:

    因为这样的事情会发生……

    只是我的两分钱:^C 显示在终端中是一件好事。确认SIGINT 已按预期发送到进程。不要试图删除它;相反,正如其他人所建议的那样,如果你真的想的话,可以开始新的一行。或者直接退出而不打印任何其他内容,就像许多其他命令行应用程序一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2021-11-02
      • 1970-01-01
      • 2011-09-14
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多