bash-like functionalities in command-line Python script
You have an interactive Python script that reads input from the command line. You want to add bash-like functionalities to it like moving the cursor with the arrows, jump to the front with Home, jump to the end with End, browse previous commands with the up arrow, etc.
You won’t believe what is needed for this:
Yes, that’s it. Just import it and you are good to go.
import readline while True: inp = raw_input("> ") print "You entered", inp
Before discovering the
readline module, I used to start my scripts with “
It does the trick too. I put the line above in a script called “
my_script.sh” and I launched this latter one. However, “
import readline” is simpler.