Clear python console command history
Question:
I am using anaconda on mac and I am wondering whether there is a way to clear my python command history. Specifically, upon calling python
in terminal, I wanted to clear anything I typed before here.
Answers:
Escape sequence?
print(chr(27) + "[2J")
Alternatively
import os
os.system('cls' if os.name == 'nt' else 'clear')
You could use subprocess.call() from the standard library subprocess module to send the reset command to the terminal which will reinitialize the terminal and in effect do something similar to clear but also clear the scrollback history.
import subprocess
subprocess.call('reset')
Assuming you want to clear the command history Python goes through when you hit the up and down arrow keys, that history is managed by either the GNU readline library or libedit, depending on your system. The Python readline
module is the Python-level interface to the underlying library (even if that library is libedit), and on systems where the underlying library supports it, you can clear the history with readline.clear_history
:
>>> import readline
>>> readline.clear_history()
I do not know if the library on your Mac supports it.
import os
os.system('cls')
This will only clear python history in this interactive session but not the previous ones.
If you are using a Linux, then:
Import os
os.system('clear')
If you’re using Windows:
Import os
os.system('CLS')
If your system doesn’t use readline
, to remove all Python command history just delete the history file at ~/.python_history
.
You can see the full path of the history file with:
python -c "import os; print(os.path.join(os.path.expanduser('~'), '.python_history'))"
.
On Windows the history file is at %userprofile%.python_history
(e.g., 'C:Users<user>.python_history'
).
If you are on Windows and nothing you found helped you clear the stored command history of your terminal, this is what helped me:
Locate and delete: %userprofile%AppDataRoamingMicrosoftWindowsPowerShellPSReadlineConsoleHost_history. txt
This will delete your entire terminal history.
I am using anaconda on mac and I am wondering whether there is a way to clear my python command history. Specifically, upon calling python
in terminal, I wanted to clear anything I typed before here.
Escape sequence?
print(chr(27) + "[2J")
Alternatively
import os
os.system('cls' if os.name == 'nt' else 'clear')
You could use subprocess.call() from the standard library subprocess module to send the reset command to the terminal which will reinitialize the terminal and in effect do something similar to clear but also clear the scrollback history.
import subprocess
subprocess.call('reset')
Assuming you want to clear the command history Python goes through when you hit the up and down arrow keys, that history is managed by either the GNU readline library or libedit, depending on your system. The Python readline
module is the Python-level interface to the underlying library (even if that library is libedit), and on systems where the underlying library supports it, you can clear the history with readline.clear_history
:
>>> import readline
>>> readline.clear_history()
I do not know if the library on your Mac supports it.
import os
os.system('cls')
This will only clear python history in this interactive session but not the previous ones.
If you are using a Linux, then:
Import os
os.system('clear')
If you’re using Windows:
Import os
os.system('CLS')
If your system doesn’t use readline
, to remove all Python command history just delete the history file at ~/.python_history
.
You can see the full path of the history file with:
python -c "import os; print(os.path.join(os.path.expanduser('~'), '.python_history'))"
.
On Windows the history file is at %userprofile%.python_history
(e.g., 'C:Users<user>.python_history'
).
If you are on Windows and nothing you found helped you clear the stored command history of your terminal, this is what helped me:
Locate and delete: %userprofile%AppDataRoamingMicrosoftWindowsPowerShellPSReadlineConsoleHost_history. txt
This will delete your entire terminal history.