Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเคลียร์ Python Shell

การเคลียร์ Python Shell (REPL)

Python Shell เรียกว่า REPL (Read-Eval-Print Loop) คำสั่งเคลียร์หน้าจอขึ้นอยู่กับ OS

Windows

import os
os.system('cls')

Linux / macOS

import os
os.system('clear')

ฟังก์ชัน Cross-platform

from os import system, name
from time import sleep

def clear():
    # Windows
    if name == 'nt':
        _ = system('cls')
    # Mac/Linux
    else:
        _ = system('clear')

print("Hello Learner!!")
sleep(5)
clear()

ใน Jupyter Notebook

# ใช้ IPython magic
%clear

# หรือ JavaScript
from IPython.display import Javascript
Javascript('IPython.notebook.clear_output()')

ใน IDE (VS Code, PyCharm)

  • VS Code: Ctrl+K ตามด้วย Ctrl+L หรือปุ่มถังขยะใน Terminal
  • PyCharm: Ctrl+L หรือปุ่ม Clear ใน Run/Debug tool window

หมายเหตุ

os.system() เรียก shell command จริง อาจไม่ทำงานในบาง environment (เช่น embedded Python)