954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Clearing the screen of text

I can't find a simple command that does this. If there's no command that does this I'm going to spam python.org until they finally agree to put this in the next version of python.

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

Python.org supposedly loves spam anyway, so long as you include some eggs.

scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

I was hoping for a more insightful answer :(

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

Sigh.

No, python doesn't have clear screen.

scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

there's not even some way to trick the computer into clearing the screen of text?

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

I suggest

>>> print("\n" * 40)

Or even better

>>> def clear():
...      print("\n"*40)
>>> clear()

Last one:

>>> def clear():
...      import sys
...      for i in range(40):
...          print(sys.ps1)
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

The last one doesn't work, says the attribute ps1 doesn't exist. But that is what I was planning to do at first, but then that leaves a big side bar. Oh well, I guess I'll just have to use the print thing.

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

Clearing the display screen in console mode depends on the Operating System, that and the limited usefulness is why many multiplatform computer languages do not include it.

Here is a Python version using module os ...

import os
# works on windows nt (also xp and vista) or linux
os.system(['clear','cls'][os.name == 'nt'])
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

That works perfect! Now the problem is really solved.

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 
import os
# works on windows nt (also xp and vista) or linux
os.system(['<strong class="highlight">clear</strong>','cls'][os.name == 'nt'])

Can anyone kindly explain the background of how this bit of code works?

Warkthogus
Newbie Poster
16 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
import os
# works on windows nt (also xp and vista) or linux
os.system(['<strong class="highlight">clear</strong>','cls'][os.name == 'nt'])

Can anyone kindly explain the background of how this bit of code works?


I suppose you pasted this from a web page. It doesn't work. The following works

import os
# works on windows nt (also xp and vista) or linux
os.system(['clear','cls'][os.name == 'nt'])

if the system is nt, then os.name=='nt' returns True, which is converted to the integer 1 and ['clear','cls'][1] is 'cls', which is passed to os.system. On another system, 'clear' is passed.
This is old style coding. Nowadays, one writes

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

(By the way, using os.system is outdated too)

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Well, I don't use that. I usually def them as function. Thanks for the post though anyway.

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 
import os
# works on windows nt (also xp and vista) or linux
os.system(['clear','cls'][os.name == 'nt'])


If [os.name == 'nt'] is True then this becomes effectively 1. So it picks item at index 1 from the list which is the Windows command 'cls'. If it's False, it becomes 0 and the item at index 0 (zero) is used, which is the Linux command 'clear'.

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You