在Python中,你可以使用os
模块和subprocess
模块来执行清屏命令
方法1:使用os
模块
import os
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
clear_screen()
方法2:使用subprocess
模块
import subprocess
def clear_screen():
subprocess.run(['cls' if os.name == 'nt' else 'clear'], check=True)
clear_screen()
这两种方法都可以在Windows和Unix系统上清屏。os.system
和subprocess.run
函数分别用于执行系统命令。在这里,我们根据操作系统的不同,选择使用cls
(Windows)或clear
(Unix)命令来清屏。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python pip命令怎么清除缓存