Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Previous Input With Up Arrow in Python Inputs [duplicate]

Problem :

Previously I wrote all of my python code on a Windows 10 machine, but more recently I've been trying to move over to MacOS. Everything is working just fine, with one exception. I'm writing a program that uses the python 'input' function and when pressing on MacOS it prints '^[[a' but on Windows 10 it prints the previous input. Is there get the previous input with the up arrow on Mac?


Example Code :

while True:
   string = input()
   print(string)

On Windows hitting will print the previous input but on Mac '^[[a' will print

like image 447
Wiazarr Avatar asked Jun 12 '26 13:06

Wiazarr


1 Answers

Use readline module to provide reading and writing of history files. More info on that in the docs

import readline

while True:
   string = input()
   print(string)
like image 173
DSteman Avatar answered Jun 15 '26 02:06

DSteman