Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách xóa Python shell (REPL)?

Python shell (REPL - Read, Evaluate, Print, Loop) cho chạy lệnh đơn lẻ. Sau nhiều lệnh, màn hình lộn xộn, cần xóa shell để hiệu quả.

Lệnh xóa terminal/shell: cls (Windows) và clear (Linux/macOS).

Windows

import os
os.system('CLS')

Linux/macOS

import os
os.system('clear')

Ví dụ: in thông điệp, chờ 5 giây, rồi xóa màn hình

from os import system, name
from time import sleep

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

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