0

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.

6
Contributors
12
Replies
13
Views
7 Years
Discussion Span
Last Post by bumsfeld
Featured Replies
  • 3

    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 ... [code=python]import os # works on windows nt (also xp and vista) or linux os.system(['clear','cls'][os.name … Read More

0

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

0

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)

Edited by Gribouillis: n/a

0

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.

3

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'])

Edited by vegaseat: n/a

Votes + Comments
Very helpful and clear.
nothing for idle ?
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?

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)

0

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

0
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'.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.