4

I'm on VS Code for Mac OS X. Is there a way that I can have VS Code automatically clear the terminal window that shows output from the Python program each time I run it? I'd like to clear the terminal window before execution.

Edit: Looking at the suggested links, I'd like to do this with VS Code tasks. I see that I'd need to edit the tasks.json file in the .vscode directory, but how would I trigger the task each time before the Python file is run, and what other parameters would I fill in for the task?

Still a little confused here. What do I put in for "command" and "label"?

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello",
            "clear": "true"
        }
    ]
}
slantalpha
  • 515
  • 1
  • 6
  • 18
  • Does this answer your question? [How to clear the interpreter console?](https://stackoverflow.com/questions/517970/how-to-clear-the-interpreter-console) – Hugo Jul 15 '20 at 16:48
  • Does this answer your question? [How do i automatically clear the terminal in VSCode before execution of script?](https://stackoverflow.com/questions/62742102/how-do-i-automatically-clear-the-terminal-in-vscode-before-execution-of-script) – soulshined Jul 15 '20 at 17:04

4 Answers4

6

You can simply import os and clear the terminal with a command.

For Windows:

import os
os.system("cls")
4

In Linux we are using:

import os
os.system("clear")
alboforlizo
  • 170
  • 1
  • 6
2

VS Code provides this machanism for developers to go further data comparation and code improvation. If you just want to show the output without interactive operations, try this:

  1. install Code Runner in extension marketplace
  2. press ctrl+, to open setting.json and add "code-runner.clearPreviousOutput": true
  3. select any code and right click, choose run code to activate the extension
  4. select the option Code in the panel and click the button to run your file, like the following picture: sample, then every time you run your project, the output will be cleared and show the latest result.
Molly Wang-MSFT
  • 7,943
  • 2
  • 9
  • 22
0

I tried what you said through the settings tasks.json. But I didn't find a places where terminal information can be automatically cleared before running Python files.

If you don't want to clear console information by entering commands, you could try these operations.

  1. Set the shortcut key 'Ctrl + K'.
  2. Use the shortcut key to open the new console.

In addition, setting "console": "external terminal" in launch.json also opens a new cmd terminal every time you run a python file.

You can refer to:This.

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25