How to clear the interpreter console?

后端 未结 30 2702
自闭症患者
自闭症患者 2020-11-21 18:15

Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff

30条回答
  •  没有蜡笔的小新
    2020-11-21 18:19

    Here are two nice ways of doing that:

    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()
    

提交回复
热议问题