2

I want to overwrite the input with a new output. I want to do the same thing (https://stackoverflow.com/a/8436827/14079038) but I need to overwrite the input.

My code returns:

while not addIp.__eq__(''):
    ignoredIp.append(addIp)
    print(addIp)
    addIp = input('Would you like to add ip ? (None = no): ')
Would you like to add ip ? (None = no): 1.1.1.1
1.1.1.1
Would you like to add ip ? (None = no): 1.1.1.2
1.1.1.2
Would you like to add ip ? (None = no):

But i want something like this:

10.11.100.66
10.11.100.67
10.11.100.68
10.11.100.69
10.11.100.70
Would you like to add ip ? (None = no):
  • 2
    why `while not addIp.__eq__(''):` ? why not normal `while not addIp == '':` or shorter `while not addIp:` – furas Aug 11 '20 at 07:30
  • 1
    I don't understand what you want to do. – furas Aug 11 '20 at 07:30
  • 2
    if you write in the same line then use `end='\r'` in `print`. But it will not work with `input()` which sends `\n` to console/terminal and you can't stop it. Maybe if you will use modules like [curses](https://docs.python.org/3.8/library/curses.html)` which can use special codes in some consoles/terminals to move cursor in any place then you could move back to previous line. – furas Aug 11 '20 at 07:33
  • 2
    You want the output on the screen to show the IP addresses only, without the questions. Is that what you want? @furas gave you the answer. – Tarik Aug 11 '20 at 07:33
  • I want to modify delete the writing of the input to the console – Simon Barras Aug 11 '20 at 07:37
  • Yes it's this @Tarik – Simon Barras Aug 11 '20 at 07:38
  • See @furas answer. – Tarik Aug 11 '20 at 07:40
  • @furas Post your answer for upvote. – Tarik Aug 11 '20 at 07:41
  • Take a look at this: https://stackoverflow.com/a/41459565/2988730. Coupled with '\r', you're all set – Mad Physicist Aug 11 '20 at 07:42

1 Answers1

2

If you write in the same line then use end='\r' in print() and it will not move to next line.

But it will not work with input() which sends \n to console/terminal and you can't stop it. Maybe if you will use modules like curses which can use special codes in some consoles/terminals to move cursor in any place then you could move back to previous line.


On Linux terminal you can use code \033[nA to move n lines up.

This way you can use \033[1A to move to previous line and print text in place of question.

It needs also some spaces at the end to remove text from longer question.

addIp = '?'
while addIp:
    addIp = input('Would you like to add ip ? (None = no): ')
    print('\033[1A' + addIp + ' '*40)

See other codes. Linux uses similar codes to colorize text.


There are also modules which should work with Windows, Linux,

and they are used to draw widgets in text mode (TUI - Text User Interface)


Not all consoles/terminals can respect these codes - often consoles in IDEs/editors don't respect these code.

furas
  • 95,376
  • 7
  • 74
  • 111
  • Excellent simple solution. Something new has been learned, thanks! – S3DEV Aug 11 '20 at 07:59
  • I use a windows system and when I write `import curses` it's make me error: `ModuleNotFoundError: No module named '_curses'` – Simon Barras Aug 11 '20 at 11:00
  • @SimonBarras I don't use Windows to test it but `curses` as standard module should work on all systems. But I found also external module [windows-curses](https://pypi.org/project/windows-curses/) and maybe it will works for your. This link has also link to [curses](https://www.lfd.uci.edu/~gohlke/pythonlibs/#curses) on page very useful for Windows users - [Unofficial Windows Binaries for Python Extension Packages](https://www.lfd.uci.edu/~gohlke/pythonlibs) – furas Aug 11 '20 at 16:14