【问题标题】:Is there any function or method to clear the running console in python program?有什么功能或方法可以清除python程序中正在运行的控制台吗?
【发布时间】:2021-03-11 00:24:03
【问题描述】:

我在 Windows 上使用 pyCharm pro。

我几乎尝试了所有方法,但没有一个有效。

我也试过这些功能:

def screen_clear():
   # for mac and linux(here, os.name is 'posix')
   if os.name == 'posix':
      _ = os.system('clear')
   else:
      # for windows platfrom
      _ = os.system('cls') 

使用这个 py 代码

i = 10
while i > 0:
    print("X"*i)
    i -= 1
    sleep(1)
    screen_clear()

及其给出的输出: Code Output

【问题讨论】:

  • 是否只需按 Enter 键即可让所有内容都消失在您的视线范围内?毕竟即使有明确的命令,你也可以在向上滚动时看到以前的输出

标签: python cmd console python-3.8


【解决方案1】:

如果您使用的是Windows Terminal,则可以:

print('\x1bc')

回应 cmets。我刚刚用原始虚拟机再次测试了这个 Windows 10 1903 下的机器,同样使用 Windows Terminal 1.4 和 Python 3.9,它按预期工作。

【讨论】:

  • 不,当我打印('\x1bc')
  • 是的,我正在使用 Windows 终端
  • @SajeelHassan 你在用什么 Windows - 查看更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2022-01-21
  • 1970-01-01
  • 2012-12-24
  • 2018-06-14
  • 2020-12-21
  • 2022-11-24
相关资源
最近更新 更多