i am trying to create a line of code that prints the first line like it normally would do then after printing it, it will then erase that output and write something else
print("This text is about to vanish - from first line",end='')
import time
time.sleep(3)
print("\rSame line output by Thirumalai")
i have this code from another user, how would i go about having it so that it shows the first line aswell, because when i run the py in terminal it doesnt do that, im using ubuntu
To make the first line appear, you need to turn off buffering. You can specify
print("This text is about to vanish - from first line",end='', flush=True)
# ~~~~~~~~~~
for the print
(Python 3.3+ needed, see here for the details and ways how to do that in older versions).
You also need to print some spaces at the end of the second print
to overwrite the remaining text.
print("\rSame line output by Thirumalai", ' ' * 15)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.