0

I want to clear the line of text created after the input() function. I've tried \033[a, \r, end='' but either I'm doing it wrong or it doesn't work. Is there a better way:

Code is:

rawguess = input('Enter a 5 letter word: ')
print('words go here')

I've tried:

rawguess = input('\033[A' + 'Enter a 5 letter word: ' + '\033[A')
print('words go here')
rawguess = input('Enter a 5 letter word: ')
print('\rwords go here')
rawguess = input('Enter a 5 letter word: ')
print('\r', end='')
print('words go here')

And other variations of these three but nothing's worked so far.

spuddo
  • 1
  • Hello @spuddo, and welcome to StackOverflow! I think, because `input` prints a newline, you need to move the cursor up 1 line _after_ `input`, instead of in the `input` prompt text. – David Aug 13 '22 at 08:14
  • Does this (https://stackoverflow.com/questions/517970/how-to-clear-the-interpreter-console) or this (https://stackoverflow.com/questions/44565704/how-to-clear-only-last-one-line-in-python-output-console) answer your question? It clears everything in the console though. – Jobo Fernandez Aug 13 '22 at 08:16
  • What operating system are you using? – Bharel Aug 13 '22 at 08:27
  • @JoboFernandez Tried a bunch of those, they either cleared the whole console or didn't do anything :/ Thanks for the reply though – spuddo Aug 13 '22 at 08:30
  • @Bharel Windows 10 – spuddo Aug 13 '22 at 08:31
  • @David Thanks for the reply! Do you know what method would do that in Python 3.9? – spuddo Aug 13 '22 at 08:32
  • I don't know how to move the cursor up in Windows. In most \*nix systems you can do `print('\x1b[1A')` but that's not supported by Windows. – David Aug 13 '22 at 08:35

2 Answers2

1

I didn't know this before, but apparently Windows now supports Console Virtual Terminal Sequences, which include using ESC [ <n> A to move the cursor up (like on *nix). So we can call the Win32 API with ctypes.windll and enable Virtual Terminal Sequences, which will allow us to use '\x1b[1A'.

import sys

# Only do this on Windows, so that *nix users
# can also run the program without errors
if sys.platform.startswith('win'):
    import ctypes
    from ctypes.wintypes import *

    STD_OUTPUT_HANDLE = -11
    ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4

    kernel32 = ctypes.windll.kernel32

    h = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    m = DWORD()
    kernel32.GetConsoleMode(h, ctypes.pointer(m))
    m.value |= ENABLE_VIRTUAL_TERMINAL_PROCESSING
    kernel32.SetConsoleMode(h, m)

rawguess = input('Enter a 5 letter word: ')
print('\x1b[1A' + 'words go here')
David
  • 816
  • 1
  • 6
  • 16
0

Well I just found a piece of dark magic allowing you to enable terminal controls in Python:

import subprocess
subprocess.run("", shell=True)
input('Enter a 5 letter word: ')
print('\x1b[1A' + 'words go here       ')
Bharel
  • 23,672
  • 5
  • 40
  • 80
  • Thanks for the trick! However, I would not rely on this feature because AFAIK it's not documented anywhere... The Win32 API _is_ documented and should work with all implementations of Python. – David Aug 13 '22 at 09:18