
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python GNU readline Interface
The readline is UNIX specific module. It defines a number of functions to read and write history files in easier way from python interpreter. We can use this module directly or using the rlcompleter module. This module settings may affect the built-in input() method prompt and also the interactive prompt.
For the MAC based system (on MAC OS X) this readline module can be implemented using the libedit library. The libedit configuration is different from GNU readline.
To use this module, we need to import the readline module in the python code
import readline
Some methods of GNU readline are as follows −
Sr.No. | Functions & Description |
---|---|
1 | readline.parse_and_bind(string) Take single line from readline init file and execute them after parsing. |
2 | readline.get_line_buffer() Get the current content of the line buffer |
3 | readline.insert_text(string) Insert text to the command line |
4 | readline.read_init_file([filename]) Parse a readline initialization file. Default value is the last provided value. |
5 | readline.read_history_file([filename]) Read history from given file. Default filename is ~/.history |
6 | readline.write_history_file([filename]) Save the history into the given file. Default file is ~/.history |
7 | readline.clear_history() Clear the current history |
8 | readline.get_history_length() Get the maximum length of the history file. |
9 | readline.set_history_length(length) Set the history file length (Number of lines) |
10 | readline. get_current_history_length () Get the total number of lines in the history file |
11 | readline.get_history_item(index) Get history item using index |
12 | readline.remove_history_item(pos) Remove history by position |
13 | readline.replace_history_item(pos, line) Replace history by position |
14 | readline.redisplay() Show the current content of the line buffer |
15 | readline.get_begidx() Getting the starting index of tab-completion scope |
16 | readline.get_endidx() Getting the ending index of tab-completion scope |
17 | readline.add_history(line) Append one line at the end of the history buffer |
This code is used to read the history file and store it inside the home directory. The code will work when it is compiled and run in interactive mode. After exiting the python shell, it will store the history file.
Example Code
import readline as rl import os import atexit my_hist_file = os.path.join(os.path.expanduser("~"), ".my_python_hist") try: rl.read_history_file(my_hist_file) rl.clear_history() except FileNotFoundError: pass print("Done") atexit.register(rl.write_history_file, my_hist_file) del os, my_hist_file
Run in interactive shell −
$ python3 Python 3.6.5 (default, Apr 1 2018, 05:46:30) [GCC 7.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> exec(open("./readline_task.py").read()) Done >>> print("readline_task.py is ececuted") readline_task.py is ececuted >>> print("History File will be updated after exit.") History File will be updated after exit. >>> 2 ** 10 1024 >>> 2 ** 20 1048576 >>> 2 ** 30 1073741824 >>> import math >>> math.factorial(6) 720 >>> exit() $ cat ~/.my_python_hist print("readline_task.py is ececuted") print("History File will be updated after exit.") 2 ** 10 2 ** 20 2 ** 30 import math math.factorial(6) exit() $
- Related Articles
- Python Completion function for GNU readline
- What are the differences between readline() and readlines() in Selenium with python?
- Difference between GNU and Unix
- Ordered Set and GNU C++ PBDS
- Python Interface to Shell Pipelines
- Python interface for SQLite databases
- Python Garbage Collector interface (gc)
- 5 Reasons Why I Hate GNU/Linux
- Command Line Interface Programming in Python?
- Python Interface to UNIX syslog library routines
- Low-level networking interface in Python (socket)
- What is the difference between Read() and ReadLine() methods in C#?
- 25 Big Companies and Devices Running on GNU/Linux
- 5 Things I Dislike and Love About GNU/Linux
- Sound-playing interface for Windows in Python (winsound)
