matlab 清楚屏幕,在shell中清除屏幕

对于OS X,您可以使用subprocess模块并从shell调用“cls”:

import subprocess as sp sp.call('cls',shell=True)

要防止在窗口顶部显示“0”,请将第二行replace为:

tmp = sp.call('cls',shell=True)

对于Linux,您必须clearreplacecls命令

tmp = sp.call('clear',shell=True)

快捷键CTRL + L呢?

它适用于所有的shell,例如Python,Bash,MySQL,MATLAB等

import os os.system('cls') # For Windows os.system('clear') # For Linux/OS X

你正在寻找的东西是在curses模块中find。

import curses # Get the module stdscr = curses.initscr() # initialise it stdscr.clear() # Clear the screen

重要的提示

重要的是要记住,在任何退出之前,您需要将terminal重置为正常模式,这可以通过以下几行完成:

curses.nocbreak() stdscr.keypad(0) curses.echo() curses.endwin()

如果你不这样做,你会得到各种奇怪的行为。 为了确保这一切都完成,我会build议使用atexit模块,如下所示:

import atexit @atexit.register def goodbye(): """ Reset terminal from curses mode on exit """ curses.nocbreak() if stdscr: stdscr.keypad(0) curses.echo() curses.endwin()

可能会做得很好。

在python中清除屏幕的一个简单方法是使用Ctrl + L,尽pipe它适用于shell以及其他程序。

使用Windows 10和pyhton3.5我已经testing了很多代码,没有什么比这更帮助我了:

首先定义一个简单的函数,这个函数将打印50个换行符;(数字50将取决于您可以在屏幕上看到多less行,所以你可以改变这个数字)

def cls(): print ("\n" * 50)

那么只要你想或需要多次调用它

cls()

这里有一些你可以在Windows上使用的选项

第一个选项:

import os cls = lambda: os.system('cls') >>> cls()

第二个选项:

cls = lambda: print('\n' * 100) >>> cls()

第三个选项,如果你在Python REPL窗口中:

Ctrl+L

该function适用​​于任何操作系统(Unix,Linux,OS X和Windows)

Python 2和Python 3

from platform import system as system_name # Returns the system/OS name from os import system as system_call # Execute a shell command def clear_screen(): """ Clears the terminal screen. """ # Clear command as function of OS command = "-cls" if system_name().lower()=="windows" else "clear" # Action system_call(command)

在Windows中,命令是cls ,在类Unix系统中,命令是clear 。

platform.system()返回平台名称。 防爆。 OS X中的'Darwin' 。

os.system()执行系统调用。 防爆。 os.system('ls -al')

如果您正在使用Linuxterminal来访问python,那么cntrl + l是清除屏幕的最佳解决scheme

import curses stdscr = curses.initscr() stdscr.clear()

子stream程允许您为Shell调用“cls”。

import subprocess cls = subprocess.call('cls',shell=True)

这很简单,我可以做到这一点。 希望对你有帮助!

您可以使用Window或Linux Os

import os os.system('cls') os.system('clear')

你可以使用subprocess模块

import subprocess as sp x=sp.call('cls',shell=True)

当我打开它们时,os.system('cls')工作正常。 它以cmd风格打开。

除了作为一个全面的伟大的CLI库之外, click还提供了一个平台不可知的clear()函数:

import click click.clear()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值