How do i automatically clear the terminal in VSCode before execution of script? [duplicate]

回眸只為那壹抹淺笑 提交于 2020-07-23 06:45:33

问题


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

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


回答1:


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')

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?




回答2:


You could import os and then at the top of your script run os.system('clear')

Example:

import os
os.system('cls||clear') # this line clears the screen 'cls' = windows 'clear' = unix
# below is my main script
print("hello world!")



回答3:


A simple native vscode solution is to take advantage of a launch.json file configurable. Specifically:

internalConsoleOptions

Controls when the internal debug console should open

redirectOutput

Both of these used in tandem will open the 'Debug Console' instead of terminal and just provide the necessary output; though it still sends to terminal should you still want it:

"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "internalConsoleOptions": "openOnSessionStart",
        "redirectOutput": true
    }
]



来源:https://stackoverflow.com/questions/62742102/how-do-i-automatically-clear-the-terminal-in-vscode-before-execution-of-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!