简体   繁体   中英

VSCode: how to automatically clear Python terminal output window before each run using VS Code tasks?

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"
        }
    ]
}

In Linux we are using:

import os
os.system("clear")

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

For Windows:

import os
os.system("cls")

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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