-1

In python 2.7, how do I not only clear screen but make the prompt begin at the top of the screen. I am surprised that I was not able to find a quick answer for this online. It seems like one of the most basic things someone would desire to be able to do when working with the shell.

If this is a duplicate then by all means direct me to original because I can't find one.

OH yeah, I am on Windows 7.

  • http://stackoverflow.com/questions/2084508/clear-terminal-in-python – Garth5689 Mar 18 '14 at 13:53
  • Specifically, [this is the answer](http://stackoverflow.com/a/2084560/2235132) from the linked question that you are looking for. – devnull Mar 18 '14 at 13:58
  • This is a duplicate question: http://stackoverflow.com/questions/517970/how-to-clear-python-interpreter-console – PythonTester Mar 18 '14 at 13:59
  • good lord, the supposed duplicates of this question suggest the most ridiculous solutions I've ever seen. I am not asking how to write an application that Python could implement in their next version release that gives the user the ability to type in a command and clear the screen while return the cursor/prompt to the top. That would be like asking, "How do I exit this room?" and somebody answering, "Well the center beam probably runs hear, so you could knock out this section of wall, and exit through that...but that only works on linux. On windows I guess you could just climb out the window – John Pendraegon Mar 18 '14 at 14:29
  • If there is not a quicker way to do this then by closing and opening the shell, then I'll just do that. That takes about 4 or 5 seconds. But the problem with that is that the session is lost. Oh well. – John Pendraegon Mar 18 '14 at 14:31
  • This is more a function of your shell window. The Linux equivalent would be pressing ctrl+L to clear the screen. – Kirk Strauser Mar 18 '14 at 14:36
  • Well, I will go ahead and assume that there is no native quick command to clear screen AND put cursor/prompt back to top of screen as if shell was newly opened. NOTE: If someone wants a clear demonstration of what I am talking about, open Windows command prompt, fill in a bunch of lines and then type: 'cls' This clears screen and situates prompt back to top left of screen. Any shell that does not have this native functionality is deficient. Somebody at Python needs to make this happen. – John Pendraegon Mar 18 '14 at 14:44

3 Answers3

1

Maybe you can try the following:

import os
os.system('cls')
vovaminiof
  • 511
  • 1
  • 4
  • 13
  • 1
    How about if the user is on Unix. Then they would have to do `os.system('clear')` so you will first have to find out the Operating system and then execute the appropriate command. – sshashank124 Mar 18 '14 at 14:01
  • That command does not do what I was inquiring into. What I would like to happen is that the python shell screen returns to how it looked when I first opened it. But I would like it to do this without closing and restarting python; and if possible, without clearing the session. – John Pendraegon Mar 18 '14 at 14:05
0

If you use the IPython shell (which has many useful features), you can just type:

clear
DBedrenko
  • 4,871
  • 4
  • 38
  • 73
0

It is best to just define a function like 'clear()' and use that to repeatedly clear the console.

from os import system
clear = lambda: system('cls') # For Windows

And now you can use 'clear()' whenever you want.