1

I apologize if this question has been answered a million times but Ive searched and tried everything to no avail. I have a simple program I created at work that copies truck stop locations to the clipboard to be pasted in emails and work orders. The shop locations are in a dictionary and use the location number as the key. It prints a list of locations with their location codes and asks you to type the number and it copies it to the clipboard. The program works great and has made life much easier, but Id like to improve one thing. The main code is in a while loop so I can leave it running and use it as needed (location list showing keys is only printed once unless you type 'list'). Problem is that I would like it to overwrite every iteration rather than print 'Type location number or 'list'', then 'location info copied to clipboard' over and over. I'd like it to display these messages and start fresh with every iteration. Ive tried sys.stdout, print(flush) and every other solution I can find. Any help would be greatly appreciated. Below is the while loop part of the program, I didn't include the dictionary named TEXT or the list of location keys as they are VERY long.

while True:
    clipboard = input("Type location number or 'list': ")

    if clipboard.lower() == 'list':
        print(locations)
    elif clipboard.isalpha() == True or clipboard.isdecimal() == False:
        print()
        print('Command not found')
        print()
    elif int(clipboard) in TEXT:
        pyperclip.copy(TEXT[int(clipboard)])
        print()
        print('Location info copied to clipboard')
        print()

    else:
        print()
        print('Location not found')
        print()
eromtap
  • 11
  • 1
  • 1
    Try using the ```ncurses``` library to move your cursor to the top of the terminal – Michael Bianconi May 14 '20 at 15:38
  • will try immediately! – eromtap May 14 '20 at 15:39
  • can you please provide your input and expected output – Ali Hassan Ahmed Khan May 14 '20 at 15:41
  • Program asks you type the location code from the list of keys. you type the code and it displays either ' location info copied ot clipboard', 'location not found' or 'command not found'. It works, but id like it to overwrite the output with each iteration instead of creating a list of these responses each time you use it. It gets very long very fast as I use it constantly. – eromtap May 14 '20 at 15:45
  • I know this isnt related to the question but all those `print()` are unnecessary, instead you can use for the first print segment: `print("\nCommand not found\n")` as `\n` is the character for a new line. – tygzy May 14 '20 at 15:49
  • lol you are absolutely right Webify, I got lazy after playing around with formatting it and just left it. I will clean that up – eromtap May 14 '20 at 15:52
  • Prehaps a duplicate of [this](https://stackoverflow.com/questions/5419389/how-to-overwrite-the-previous-print-to-stdout-in-python/5419488), [this](https://stackoverflow.com/questions/5290994/remove-and-replace-printed-items) and [this](https://stackoverflow.com/questions/3249524/print-in-one-line-dynamically). Try posting what you tried with `sys.stdout` and `flush` with any error messages so we can better help you out – bug_spray May 14 '20 at 17:23
  • I had read those posts and with sys.stdout was able to get print functions to overwrite just writing a test script. The issue really seems to be the input. I know sys.stdin is used for input, but for the life of me I cant figure out how to utilize it to overwrite. is there something like sys.stdout.flush() for sys.stdin? I can post specifics when I get back to my computer if more info like specific errors is required mostly I get "io.textIOWrapper' object not callable' when I try to use sys.stdin on the input and 'outstream' when using stdout. Flush really resulted in no change to output – eromtap May 14 '20 at 17:58

0 Answers0