Is there a way to clear your printed text in python?

后端 未结 5 1341
野的像风
野的像风 2020-12-10 16:12

I have wanted for a long time to find out how to clear something like print(\"example\") in python, but I cant seem to find anyway or figure anything out.

pr         


        
相关标签:
5条回答
  • 2020-12-10 16:38

    I think this is what you want to do:

    take the cursor one line up and delete the line

    this can be done like using the code below

    import sys
    import time
    
    def delete_last_line():
        "Use this function to delete the last line in the STDOUT"
    
        #cursor up one line
        sys.stdout.write('\x1b[1A')
    
        #delete last line
        sys.stdout.write('\x1b[2K')
    
    
    ########## FOR DEMO ################
    if __name__ == "__main__":
        print("hello")
        print("this line will be delete after 2 seconds")
        time.sleep(2)
        delete_last_line()
    ####################################
    
    0 讨论(0)
  • 2020-12-10 16:43
    import os
    os.system('cls')
    

    Or os.system('clear') on unix (mac and linux). If you don't want the scroll up either, then you can do this:

    os.system("printf '\033c'") should get rid of scroll back too. Something that works on all systems:

    import os
    os.system('cls' if os.name == 'nt' else "printf '\033c'")
    
    0 讨论(0)
  • 2020-12-10 16:45
    import os
    os.system('clear')
    

    It Works. Clears your thing Just Fine.

    0 讨论(0)
  • 2020-12-10 16:48
    import time
    Dots = "."
    total = 0
    count = 0
    while count < 5:
        if total<1:
            print("[+]Saving" + Dots,end="")
        total+=1
        time.sleep(1)
        print(Dots,end="")
    

    This code works perfectly fine...

    0 讨论(0)
  • 2020-12-10 16:58

    Well; i have a temporary way:-

    print('Hey', end = '')
    
    for i in range(4):
        print('\b', end = '')
    print('How is your day?')
    
    0 讨论(0)
提交回复
热议问题