Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Everyone!
I don't know if anyone knows how to do this, or even if this is the right place to ask this. But how would one clear the output console using the print() function in python?
I haven't the slightest clue how where to start with this question, but i do have the complete python reference for python 3.3.0 and yes, i do use python 3.3.0

Some background info on this question:
i was coding in blender(i code and debug python scripts in the blender game engine(version 2.68.0) last week and i needed to print something to the console and have blender clear the debugging console when ever i press a specific key.
now i know how to set up the key stroke command and all of that but what i don't know how to do is the part where i actually clear the console via the print() function.
having this ability would greatly improve my debugging skills in python, because sometimes when i am debugging a large script i can have a lot of information being printed onto the debugging console, and it gets kind of messy when there is no line breaks.
thanks for your help in advance,
MasterCodeon
Posted
Updated 19-Dec-14 7:16am
v3

Instead of using print(), it might be easier to use a system call (though apparently, using print is possible, see PIEBALDconsult's answer). To execute a system call, use os.system.
Python
import os
os.system('cls')

cls is Windows-only. If you use Linux or Mac OS, use clear instead.

How does this work? os.system executes a system call; so it calls the cls command which clears the screen.
 
Share this answer
 
v3
Comments
MasterCodeon 19-Dec-14 10:57am    
how does this work?
i mean i know what cls does in windows but how would running that command on a python console work, i would think the 2 would be incompatible.
MasterCodeon 19-Dec-14 11:00am    
wow that seemed kind of complicated in my first comment
uh what i meant to say is what goes on under the hood with this command(I know what the import os does though)
Thomas Daniels 19-Dec-14 11:04am    
I edited my answer.
MasterCodeon 19-Dec-14 11:11am    
huh, so if i replaced the 'cls' with 'start' (or whatever CMD command i wanted) and use the correct arguments i could open a new CMD window or what ever i specified right?
and if i could open a new CMD window would it open it in the python console or would it just open up in a new window?
nevertheless very cool,
thank you for your help
ProgramFOX, i shall have a much easer time debuging python scripts now
Thank you.
Thomas Daniels 19-Dec-14 11:16am    
Yes, you can call those commands using os.system.

I have tried executing cmd using os.system, and based on my tests, cmd opens in the Python console.
Supposedly ANSI escape sequences should still work on Windows, but I haven't gotten it to work.

http://forum.codecall.net/topic/59142-how-to-clear-the-console-screen-with-ansi-any-
language/
[^]

P.S.
From some of my old C code (circa 1995), for working with a VT100-compatible dumb terminus: printf("\033[H\033[J")
If the above blog entry is correct, this should work on Windows 7, but I wonder whether or not ANSI compatibility has to be enabled or something.
No further research and testing on Windows 7 has found that this succeeds.

I couldn't get this to work either: http://support.microsoft.com/kb/101875[^]
 
Share this answer
 
v4
Comments
MasterCodeon 19-Dec-14 11:14am    
sorry i cant't go to any links other than here on codeproject(this is due to my system admin, he/she has all the net blocked except a handful of links(Microsoft, MSDN and codeproject being a few necessity's)
PIEBALDconsult 19-Dec-14 11:20am    
I'll try to add more later if I can get it to work at home.
MasterCodeon 19-Dec-14 11:23am    
ok cool, thanks.
MasterCodeon 19-Dec-14 11:24am    
i have no idea what this would even entail though but if/when you add more i might understand.
MasterCodeon 19-Dec-14 11:25am    
oh and what is a "VT100"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900