V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zictos
V2EX  ›  Python

Python 如何显示倒计时(同一行自动更新)?

  •  
  •   zictos · 2020-09-24 08:33:33 +08:00 · 1990 次点击
    这是一个创建于 1309 天前的主题,其中的信息可能已经有所发展或是发生改变。

    类似于 windows 的 cmd 命令的 timeout 命令,比如在 cmd 窗口输入“timeout 10”就会显示 10 秒的倒计时(是在同一行更新,不会每 1 秒就显示一个新行)。如图:

    avatar



    直接在 python 中调用 os.system('timeout 10')用在多线程或多进程中会有 bug,所以希望有 python 原生的方式。

    zmxnv123
        1
    zmxnv123  
       2020-09-24 08:38:19 +08:00 via iPhone
    退格符了解下
    keith1126
        2
    keith1126  
       2020-09-24 08:40:03 +08:00
    \r
    jackmod
        3
    jackmod  
       2020-09-24 08:56:52 +08:00
    print(f'wait {sec} seconds', end = '\r')
    zictos
        4
    zictos  
    OP
       2020-09-24 09:49:36 +08:00
    import time
    for i in range(10,0,-1):
    print('还需等待%s 秒' % i, end = '\r')
    time.sleep(1)
    print('还需等待 0 秒',end = '\r')
    zictos
        5
    zictos  
    OP
       2020-09-24 09:51:16 +08:00
    import time
    for i in range(10,0,-1):
    [缩进]print('还需等待%s 秒' % i, end = '\r')
    [缩进]time.sleep(1)
    print('还需等待 0 秒',end = '\r')
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3272 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:08 · PVG 21:08 · LAX 06:08 · JFK 09:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.