- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Some methods of GNU readline are as follows −
|Sr.No.||Functions & Description|
Take single line from readline init file and execute them after parsing.
Get the current content of the line buffer
Insert text to the command line
Parse a readline initialization file. Default value is the last provided value.
Read history from given file. Default filename is ~/.history
Save the history into the given file. Default file is ~/.history
Clear the current history
Get the maximum length of the history file.
Set the history file length (Number of lines)
readline. get_current_history_length ()
Get the total number of lines in the history file
Get history item using index
Remove history by position
Replace history by position
Show the current content of the line buffer
Getting the starting index of tab-completion scope
Getting the ending index of tab-completion scope
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.
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() $
- Python Completion function for GNU readline
- Ordered Set and GNU C++ PBDS
- What are the differences between readline() and readlines() in Selenium with python?
- Python Interface to Shell Pipelines
- Python interface for SQLite databases
- Python Garbage Collector interface (gc)
- Command Line Interface Programming in Python?
- What is the difference between Read() and ReadLine() methods in C#?
- Python Interface to UNIX syslog library routines
- Low-level networking interface in Python (socket)
- What are the differences between -std = c++11 and -std = gnu++11?
- What is the difference between Read(), ReadKey() and ReadLine() methods in C#?
- Sound-playing interface for Windows in Python (winsound)
- Why an interface cannot implement another interface in Java?
- What is the user interface and operating system interface?