Profile

Click to view full profile
Hi, I'm Veerapat Sriarunrungrueang, an expert in technology field, especially full stack web development and performance testing.This is my coding diary. I usually develop and keep code snippets or some tricks, and update to this diary when I have time. Nowadays, I've been giving counsel to many well-known firms in Thailand.
view more...

Friday, October 22, 2010

How to clear screen in Python Interactive Shell

Most of the time when I deal with interactive mode, there are a lot of lines. , this is a simply solution to solve this problem.
There are 2 methods
1. Short Key: Under Linux/UNIX system (on x86 at least) you can use the CTRL+L
2. Writing a code:

import os
if os.name == "posix":
# Unix/Linux/MacOS/BSD/etc
os.system("clear")
elif os.name in ("nt", "dos", "ce"):
# DOS/Windows
os.system("CLS")
 
Note that for writing a code and you know your os.
You can just "import os" then "os.system([command to clear screen for that os])".

p.s. os.system() is very useful to make python work with other command in command line mode ex. os.system("python") to call python shell in python shell.

2 comments:

  1. how do you implement this in python shell? I tried every combo but it does not work

    ReplyDelete
    Replies
    1. I implemented nothing special. It's just a command to let python call a function in command line. For example, in Windows command prompt, "CLS" is a command to clear screen. Then, we let to make python call it using os.system("CLS").

      So, in your case if you know the command to clear screen of your system. I think it should be work.

      Delete