如何在执行脚本之前自动清除 VSCode 中的终端?

问题描述:

我目前正在使用 VS Code 来学习 Python.所以我必须每分钟运行 10-15 次脚本,只需要做一些小的编辑并学习所有东西.我在 VS 代码的集成终端中运行脚本

I am currently using VS Code to learn Python . So i have to run scripts like 10-15 times a minute , just doing small edits and learning all things. I am running the scripts in integrated terminal of VS code

显然终端变得非常混乱,我必须始终通过运行 clear 手动清除终端,我希望每次执行 Python 脚本时都能自动清除终端.请注意,我不是在寻找清除终端的键盘快捷键,而是需要终端在显示当前脚本的输出之前自动清除旧输出.我找不到任何可以做到这一点的东西,希望有人能提供出路

So apparently the terminal gets horribly cluttered and i have to always clear the terminal manually by running clear, I want the terminal to automatically get cleared every time I execute the Python script . Please note that I am not looking for a keyboard shortcut to clear terminal , rather I need the terminal to automatically clear the old output before displaying the output of the present script. I couldn't find anything that does this , hopefully someone can provide a way out

基于@Jacques 答案,您可以使用 sys 模块动态执行平台检查:

Building on @Jacques answer, you can use the sys module to dynamically perform a platform check:

import os
import sys

# Linux
if sys.platform.startswith('linux'):
    os.system('clear')
# Windows
elif sys.platform.startswith('win32'):
    os.system('cls')

或者,请参阅这篇关于配置 vscode 为所有程序执行此操作的帖子:如何在开始构建时自动清除 VS Code 终端?

Alternatively, see this post on configuring vscode to do this for all programs: How do I automatically clear VS Code terminal when starting a build?