How to clear python console (i.e. Ctrl+L command line equivalent)

2024/11/15 12:58:03

OS = Linux

[boris@E7440-DELL ~]$ uname -a
Linux E7440-DELL 3.17.4-200.fc20.x86_64 #1 SMP Fri Nov 21 23:26:41 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

From python console (Spyder 2.2.4, Python 2.7.5 64bits, Qt 4.8.5) it is seen as:

>>> import os
>>> print(os.name)
posix

I'm trying to find out a way to clear python console. Not just any solution is suitable, but it must be exactly same result as pressing Ctrl+L.

From other threads I have already tried several options:

>>> import os
>>> os.system('clear')
256>>> import subprocess
>>> subprocess.call("clear", shell=True)
1>>> print '\n'*1000

As you can see neither os.system('clear') nor subprocess.call("clear", shell=True) produce desired result. They just output a value (256 or 1 respectively). print '\n'*1000 is so far closest the desired outcome. However, there are two issues with it:

  1. the cursor is not at the top of the screen (as Ctrl+L does), but it stays at the bottom, so all new lines printed by my code are being scrolled upwards, which makes it impossible to read.
  2. the visual experience is highly dependent on the value, so in order to make it somewhat readable I have to use print '\n'*100000 instead

Does anyone know the right solution, the one that can really do Ctrl+L from the command line? (yes I am using linux, and I have no interest in windows solutions)

Answer

You can try:

os.system('tput reset')

To hide the return value, use:

variable = os.system('tput reset')
https://en.xdnf.cn/q/119417.html

Related Q&A

Floating point accuracy in python

Any reason why c shouldnt equal 0.321?>>> from math import ceil >>> a = 123.321 >>> b = a % 60 >>> b 3.320999999999998 >>> ceil(b) 4.0 >>> c = cei…

datetime64 comparison in dataframes

I am struggling with datetime64 comparisons in dataframes to update a column. lets say we have a dataframe df with a date columndf.date.values[0] Out[128]: numpy.datetime64(2015-05-17T22:00:00.00000000…

Relative import of a apackage in python flask application

Trying to make the sample flask application more modular,I am new to python and flask trying to build a sample application where , I have planned to maintain the folder structure of the application a…

Same sparql not returning same results

Im using the same sparql statement using two different clients but both are not returning the same results. The owl file is in rdf syntax and can be accessed here. This is the sparql statement: PREFIX …

Accessing nested values in nested dictionaries in Python 3.3

Im writing in Python 3.3. I have a set of nested dictionaries (shown below) and am trying to search using a key at the lowest level and return each of the values that correspond to the second level. Pa…

scrape site with anti forgery token

Im trying to scrape data from website that uses anti forgery token what i tried to do is sending a get request then finding the key and use it to send a post request i was able to successfully scrape t…

Pandas merge and grouby

I have 2 pandas dataframes which looks like below. Data Frame 1: Section Chainage Frame R125R002 10.133 1 R125R002 10.138 2 R125R002 10.143 3 R125R002 10.148 4 R125R002 …

Find a pattern in the line of another file in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

AssertionError if running code in Python prompt but not if running as file

Why trying to explain here on stackoverflow what the Python command id() does and how can it be used to reveal how Python works under the hood I had run into following strange behavior I am struggling …

Remove values before and after special character

I have a dataframe, df, where I would like to remove the values that come before the underscore _ and after the underscore _ , essentially, keeping the middle. Also keeping the digits at the end and co…