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

Clear the console screen

Hi Bumsfeld, thanks for sharing, I haven't come across this before

# works on Windows or Linux, also Vista
import os
os.system(['clear','cls'][os.name == 'nt'])



Can please tell me how is the following code is working. I mean,

>>> ['clear','cls'] [True]
'cls'
>>> ['clear','cls'] [False]
'clear'
>>> ['a','b']['some_string' == r'some_string']
'b'
>>> ['a','b']['some_string' == r'somestring']
'a'
>>>


['a','b'] is list and [True] is also another list, what is happening here? I have no knowledge on this, could please let me know what is happening?

Thanks in advance,
regards,
kath.

Sorry kath, moved this out of 'Starting Python' the rules there are ...The idea of this thread is to help the beginning Python programmer with hints and helpful code. Please feel free to contribute! If you have any questions start your own thread!vegaseat

katharnakh
Posting Whiz in Training
237 posts since Jan 2006
Reputation Points: 19
Solved Threads: 34
 

Hi Bumsfeld,

# works on Windows or Linux, also Vista
import os
os.system(['clear','cls'][os.name == 'nt'])

I got it.. I was thinking ['clear','cls'] is list of string and [True] is a list of boolean. But the later is wrong. Later is a index, True will cast into 1 , hence that can deduced as

>>os.system(['clear','cls'][os.name == 'nt'])
>>'cls'
# [os.name == 'nt'] this part will result in either True or False
# hence we can rewrite the above line as
>>os.system(['clear', 'cls'][True]) # for eg:
# which is nothing but os.system(['clear', 'cls'])[1]
>>'cls'


Thanks a lot for this piece of code.. really I enjoyed it..


Cheers,
kath :icon_smile:

katharnakh
Posting Whiz in Training
237 posts since Jan 2006
Reputation Points: 19
Solved Threads: 34
 

Nice deduction there kath! Python does equate True with 1 and False with 0.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Hi Vegaseat,

Sorry kath, moved this out of 'Starting Python' the rules there are ...
vegaseat

No need to be sorry... Well, thanks for you to reminding me.

Cheers,
kath.

katharnakh
Posting Whiz in Training
237 posts since Jan 2006
Reputation Points: 19
Solved Threads: 34
 

How can i control the position of the cursor ? something like this maybe:

print  "\n"*500
setCursor(0, 0)
StrikerX11
Light Poster
27 posts since Jun 2007
Reputation Points: 10
Solved Threads: 7
 

If you want to do that sort of thing, you might as well download the console module from:
http://effbot.org/downloads/

Console interface for Windows only (versions up to Python25 available):
console-1.1a1-20011229.win32-py2.5.exe

Look at documentation at:
http://effbot.org/zone/console-handbook.htm

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

Thanks, That's is helpful.

StrikerX11
Light Poster
27 posts since Jun 2007
Reputation Points: 10
Solved Threads: 7
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You