在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)命令来清屏。