Jump to content

Hi all!

I wish to share the discovery i made for deleting the last line in the console on python 3, I hope this can help someone, any questions please redirect to me,

Thanks,

Blake

 

 

code:

import os 
import sys
import time


#last line deletion
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')
    
    
###DEMO###
print("this line will delete in 5 seconds")
time.sleep(5)
delete_last_line()
 

 

The link to the github with the code and more is :

https://github.com/Blake-McCullough/LastLinePython3Delete/tree/main

 

Attached is also the file for deleting the last line if u wish to just download it.

Last line python 3 delete.py

Edited by Spoiled_Kitten
adding text code for others to see instead of visiting link or opening file.

UwU

Link to comment
https://linustechtips.com/topic/1257798-delete-last-line-in-console-python-3/
Share on other sites

Link to post
Share on other sites

Why not just post the code in the forum rather than linking and posting random files

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

  • 4 years later...
On 10/15/2020 at 9:36 AM, Spoiled_Kitten said:

Hi all!

I wish to share the discovery i made for deleting the last line in the console on python 3, I hope this can help someone, any questions please redirect to me,

Thanks,

Blake

 

 

code:

import os 
import sys
import time


#last line deletion
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')
    
    
###DEMO###
print("this line will delete in 5 seconds")
time.sleep(5)
delete_last_line()
 

 

The link to the github with the code and more is :

https://github.com/Blake-McCullough/LastLinePython3Delete/tree/main

 

Attached is also the file for deleting the last line if u wish to just download it.

Last line python 3 delete.py 351 B · 0 downloads

import sys

import time

 

def delete_last_line():

    """Use this function to delete the last line in the STDOUT"""

 

    # Move cursor up one line

    sys.stdout.write('\x1b[1A')

 

    # Delete the line

    sys.stdout.write('\x1b[2K')

 

    # Flush to apply the changes

    sys.stdout.flush()

 

print("This line will delete in 5 seconds")

time.sleep(5)

delete_last_line()

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×