Clearing the screen of text

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2008
Posts: 965
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #11
Oct 20th, 2009
Originally Posted by Warkthogus View Post
  1. import os
  2. # works on windows nt (also xp and vista) or linux
  3. 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
  1. import os
  2. # works on windows nt (also xp and vista) or linux
  3. 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
  1. os.system('cls' if os.name == 'nt' else 'clear')
(By the way, using os.system is outdated too)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #12
Oct 20th, 2009
Well, I don't use that. I usually def them as function. Thanks for the post though anyway.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #13
Oct 20th, 2009
  1. import os
  2. # works on windows nt (also xp and vista) or linux
  3. 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'.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC