python如何清空输出内容_如何在python输出控制台中只清除最后一行?

我只想清除输出控制台窗口的最后几行。为了实现这一点,我决定使用create stopwatch,我已经实现了在键盘中断时中断,按enter键时创建lap,但我的代码只创建lap一次,而我的当前代码正在清除整个输出屏幕。

清除.pyimport 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)

当我跑的时候%run /clear.py

在我的iPythonShell中,我只能创造一圈,但它并不是永久的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值