0
我想从输出控制台窗口只清除最后几行。为了达到这个目的,我决定使用创建秒表,并且我已经实现了在键盘中断和输入键按下它创建圈,但我的代码只创建圈一次,我目前的代码是清除整个输出屏幕。如何只清除python输出控制台中的最后一行?
clear.py
import os
import msvcrt, time
from datetime import datetime
from threading import Thread
def threaded_function(arg):
while True:
input()
lap_count = 0
if __name__ == "__main__":
# thread = Thread(target = threaded_function)
# thread.start()
try:
while True:
t = "{}:{}:{}:{}".format(datetime.now().hour, datetime.now().minute, datetime.now().second, datetime.now().microsecond)
print(t)
time.sleep(0.2)
os.system('cls||clear') # I want some way to clear only previous line instead of clearing whole console
if lap_count == 0:
if msvcrt.kbhit():
if msvcrt.getwche() == '\r': # this creates lap only once when I press "Enter" key
lap_count += 1
print("lap : {}".format(t))
time.sleep(1)
continue
except KeyboardInterrupt:
print("lap stop at : {}".format(t))
print(lap_count)
,当我在我的IPython shell中运行
%run <path-to-script>/clear.py
我可以只创建一个圈,但它没有停留永久。
'\ r'正在打印新的空行,而不是删除前行。 – Gahan
使上一行不要在最后加上分号来打印换行符,您是否注意到代码中? –
尝试在无限while循环或for循环中编写自己的代码并重复多次,您将看到它正在创建新行。正如标签中提到的,我对python3有所了解 – Gahan