Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何清除解释器控制台?_Python_Windows_Console - Fatal编程技术网

Python 如何清除解释器控制台?

Python 如何清除解释器控制台?,python,windows,console,Python,Windows,Console,与大多数Python开发人员一样,我通常会打开一个控制台窗口,让Python解释器运行以测试命令、dir()stuff、help()stuff,等等 与任何控制台一样,一段时间后,过去的命令和打印的可见积压会变得杂乱无章,有时在多次重新运行同一命令时会令人困惑。我想知道是否以及如何清除Python解释器控制台 我听说过在Windows上进行系统调用,或者调用cls,或者在Linux上调用clear,但我希望我能命令解释器自己做些什么 注意:我在Windows上运行,所以Ctrl+L不起作用。好吧

与大多数Python开发人员一样,我通常会打开一个控制台窗口,让Python解释器运行以测试命令、
dir()
stuff、
help()stuff
,等等

与任何控制台一样,一段时间后,过去的命令和打印的可见积压会变得杂乱无章,有时在多次重新运行同一命令时会令人困惑。我想知道是否以及如何清除Python解释器控制台

我听说过在Windows上进行系统调用,或者调用
cls
,或者在Linux上调用
clear
,但我希望我能命令解释器自己做些什么


注意:我在Windows上运行,所以
Ctrl+L
不起作用。

好吧,这里有一个快速的解决方法:

>>> clear = "\n" * 100
>>> print clear
>>> ...do some other stuff...
>>> print clear
或者,要保存一些键入,请将此文件放在python搜索路径中:

# wiper.py
class Wipe(object):
    def __repr__(self):
        return '\n'*1000

wipe = Wipe()
然后,您可以在解释器中随意执行此操作:)


如您所述,您可以执行系统调用:

对于Windows:

>>> import os
>>> clear = lambda: os.system('cls')
>>> clear()
对于Linux,它将是:

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

使用空闲。它有许多方便的功能。例如,Ctrl+F6将重置控制台。关闭和打开控制台是清除它的好方法。

编辑:我刚刚读了“windows”,这是给linux用户的,对不起


在bash中:

#!/bin/bash

while [ "0" == "0" ]; do
    clear
    $@
    while [ "$input" == "" ]; do
        read -p "Do you want to quit? (y/n): " -n 1 -e input
        if [ "$input" == "y" ]; then
            exit 1
        elif [ "$input" == "n" ]; then
            echo "Ok, keep working ;)"
        fi
    done
    input=""
done
将其保存为“whatyouwant.sh”,chmod+x然后运行:

./whatyouwant.sh python
或者python以外的东西(空闲的,随便什么)。 这将询问您是否确实要退出,如果不想退出,将重新运行python(或您作为参数给出的命令)

这将清除所有,屏幕和所有变量/对象/您在python中创建/导入的任何内容


在python中,当您想退出时,只需键入exit()。

这里有一个更方便的跨平台工具

import os

def cls():
    os.system('cls' if os.name=='nt' else 'clear')

# now, to clear the screen
cls()

雨刮器很酷,好的是我不用在它周围打“()”。 这里有一些细微的变化

# wiper.py
import os
class Cls(object):
    def __repr__(self):
        os.system('cls')
        return ''
用法非常简单:

>>> cls = Cls()
>>> cls # this will clear console.

虽然这是一个较老的问题,但我认为我应该总结一下我认为最好的其他答案,并建议您将这些命令放入一个文件中,并将PYTHONSTARTUP环境变量设置为指向它,这是我自己的一个补充。因为我现在在窗户上,它有点偏向那个方向,但很容易向其他方向倾斜

下面是我找到的一些描述如何在Windows上设置环境变量的文章:




顺便说一句,即使文件中有空格,也不要在文件路径周围加引号

无论如何,下面是我对放入(或添加到现有)Python启动脚本的代码的看法:

# ==== pythonstartup.py ====

# add something to clear the screen
class cls(object):
    def __repr__(self):
        import os
        os.system('cls' if os.name == 'nt' else 'clear')
        return ''

cls = cls()

# ==== end pythonstartup.py ====
顺便说一句,您还可以使用@
技巧将
exit()
更改为just
exit
(其别名同上
quit
):

最后,这里还有一些将主解释器提示从
>
更改为cwd+
>

class Prompt:
    def __str__(self):
        import os
        return '%s >>> ' % os.getcwd()

import sys
sys.ps1 = Prompt()
del sys
del Prompt

这应该是跨平台的,并且根据使用首选的
子流程调用
,而不是
操作系统
。应在Python>=2.4中工作

import subprocess
import os

if os.name == 'nt':
    def clearscreen():
        subprocess.call("cls", shell=True)
        return
else:
    def clearscreen():
        subprocess.call("clear", shell=True)
        return

这里有两种很好的方法:

1.

import os

# Clear Windows command prompt.
if (os.name in ('ce', 'nt', 'dos')):
    os.system('cls')

# Clear the Linux terminal.
elif ('posix' in os.name):
    os.system('clear')
2.

import os

def clear():
    if os.name == 'posix':
        os.system('clear')

    elif os.name in ('ce', 'nt', 'dos'):
        os.system('cls')


clear()

这个怎么样

- os.system('cls')

这差不多是最短的了

我正在Windows XP、SP3上使用MINGW/BASH

(把这个放进去。pythonstartup)
#我的ctrl-l已经起作用了,但这可能会帮助其他人
#在窗口底部保留提示…
导入读线
readline.parse_和_bind('\C-l:清除屏幕')

#这在BASH中是有效的,因为我在.inputrc中也有它,但对于某些
#当我进入Python时它被删除的原因
readline.parse_和_bind('\C-y:kill whole line')


我再也受不了输入“exit()”,对martineau/Triptych的技巧感到高兴:

不过我稍微修改了一下(把它放进了.pythonstartup)



Linux中的OS命令
clear
和Windows中的
cls
输出一个“魔术字符串”,您可以直接打印出来。要获取字符串,请使用popen执行命令,并将其保存在变量中以供以后使用:

from os import popen
with popen('clear') as f:
    clear = f.read()

print clear
在我的机器上,字符串是
'\x1b[H\x1b[2J'

>>> ' '*80*25
更新:80x25不太可能是控制台窗口的大小,因此要获得真正的控制台维度,请使用模块中的函数。Python不提供任何与核心发行版类似的功能

>>> from pager import getheight
>>> '\n' * getheight()
以下是将所有其他答案合并到一起的功能。功能:

  • 您可以将代码复制粘贴到shell或脚本中
  • 您可以随心所欲地使用它:

    >>> clear()
    >>> -clear
    >>> clear  # <- but this will only work on a shell
    
  • 您可以将其作为脚本调用:

    $ python clear.py
    
  • 如果它不能识别您的系统,它将是真正的多平台 (
    ce
    nt
    dos
    posix
    )它将返回到打印空行


  • 您可以在此处下载[完整]文件:
    或者,如果您只是在寻找代码:

    class clear:
     def __call__(self):
      import os
      if os.name==('ce','nt','dos'): os.system('cls')
      elif os.name=='posix': os.system('clear')
      else: print('\n'*120)
     def __neg__(self): self()
     def __repr__(self):
      self();return ''
    
    clear=clear()
    
    我是python新手(真的很新),在我阅读的一本书中,我了解了他们教授的语言,他们教我如何创建这个小函数来清除控制台中可见的积压工作以及过去的命令和打印:

    打开shell/创建新文档/创建函数,如下所示:

    def clear():
        print('\n' * 50)
    
    将它保存在python目录的lib文件夹中(我的是C:\Python33\lib) 下次您需要清除控制台时,只需使用以下命令调用函数:

    clear()
    
    就这样。
    PS:你可以随意命名你的函数。我见过人们使用“wiper”“wipe”和变体。

    我发现最简单的方法就是关闭窗口并运行模块/脚本来重新打开外壳。

    我的方法是编写这样一个函数:

    import os
    import subprocess
    
    def clear():
        if os.name in ('nt','dos'):
            subprocess.call("cls")
        elif os.name in ('linux','osx','posix'):
            subprocess.call("clear")
        else:
            print("\n") * 120
    
    然后调用
    clear()
    清除屏幕。 这适用于windows、osx、linux、bsd…所有操作系统。

    我不确定windows的“shell”是否支持此功能,但在linux上:

    打印“\033[2J”

    在我看来
    $ python clear.py
    
    class clear:
     def __call__(self):
      import os
      if os.name==('ce','nt','dos'): os.system('cls')
      elif os.name=='posix': os.system('clear')
      else: print('\n'*120)
     def __neg__(self): self()
     def __repr__(self):
      self();return ''
    
    clear=clear()
    
    def clear():
        print('\n' * 50)
    
    clear()
    
    import os
    import subprocess
    
    def clear():
        if os.name in ('nt','dos'):
            subprocess.call("cls")
        elif os.name in ('linux','osx','posix'):
            subprocess.call("clear")
        else:
            print("\n") * 120
    
    Press CTRL + L
    
    import os
    cls = lambda: os.system('cls')
    cls()
    
    cls = lambda: print('\n'*100)
    cls()
    
    import os
    os.system('cls') # Windows
    os.system('clear') # Linux, Unix, Mac OS X
    
    print("\033[H\033[J")
    
    import os
    clear = lambda: os.system('cls' if os.name=='nt' else 'clear')
    clear()
    
    import subprocess   
    clear = lambda: subprocess.call('cls||clear', shell=True)
    clear()
    
    >>> import os
    >>> clear = lambda: os.system('clear')
    >>> clear()
    
    import os
    os.system('clear')
    
    os.system('cls')
    
    # Clear or wipe console (terminal):
    # Use: clear() or wipe()
    
    import os
    
    def clear():
        os.system('clear')
    
    def wipe():
        os.system("clear && printf '\e[3J'")
    
    cls = lambda: print("\033c\033[3J", end='')
    cls()
    
    print("\033c\033[3J", end='')