简体   繁体   English

为什么os.system('cls') 不清除最近的output?

[英]Why doesn't os.system('cls') clear the recent output?

I am always using system('cls') in C language before using Dev-C++.在使用 Dev-C++ 之前,我总是在 C 语言中使用system('cls') Now I am studying Python and using Pycharm 2020.2.3.现在我正在研究 Python 并使用 Pycharm 2020.2.3。

I tried to use os.system('cls') .我尝试使用os.system('cls') Here is my program:这是我的程序:

import os

print("clear screen")
n = int(input(""))
if n == 1:
    os.system('cls')

There is no error in my program but it is not clearing the recent output.我的程序没有错误,但它没有清除最近的 output。

This is the output of my program:这是我的程序的 output:

我的输出截图

What seems to be the problem why it is not clearing the recent output?为什么它没有清除最近的 output 似乎是什么问题?

This code works perfectly well.这段代码运行良好。 I think there is a problem how you are running it.我认为您的运行方式存在问题。 you should run it with commmand prompt.您应该使用命令提示符运行它。

if still not working.如果仍然无法正常工作。

os.system('cls')  # on windows
os.system('clear')  # on linux / Mac

You can test your code here https://www.python.org/shell/你可以在这里测试你的代码https://www.python.org/shell/

PhCharm displays the output of your running module using the output console. PhCharm 使用 output 控制台显示正在运行的模块的 output。 In order for your terminal commands under os.system() to work, you need to emulate your terminal inside the output console.为了使os.system()下的终端命令正常工作,您需要在 output 控制台中模拟您的终端。

  1. Select 'Edit Configurations' from the 'Run' menu. Select “运行”菜单中的“编辑配置”。
  2. Under the 'Execution' section, select 'Emulate terminal in output console'在“执行”部分下,select“在 output 控制台中模拟终端”

JetBrains Sergey Karpov Adds : JetBrains 谢尔盖卡尔波夫补充说

Our Run window doesn't support some of the things that one can do in the terminal.我们的 Run window 不支持一些可以在终端中执行的操作。 One of them is clearing the output.其中之一是清除 output。

When trying to clear the output in a 'non-terminal' output window, PyCharm even shows that the TERM environment variable is not set . When trying to clear the output in a 'non-terminal' output window, PyCharm even shows that the TERM environment variable is not set .

Setting that variable manually may help in some cases, but other terminal-specific things are still missing, which is why we have an option to emulate the terminal in the Run window.在某些情况下手动设置该变量可能会有所帮助,但仍然缺少其他特定于终端的内容,这就是为什么我们可以选择在 Run window 中模拟终端的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM