推荐学习书目
› 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 · Sep 24, 2020 · 2872 views
    This topic created in 2242 days ago, the information mentioned may be changed or developed.

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

    avatar



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

    xe2vherd
    xe2vherd
    Sep 24, 2020 via iPhone
    1
    退格符了解下
    zackwu
    zackwu
    Sep 24, 2020
    2
    \r
    jackmod
    jackmod
    Sep 24, 2020
    3
    print(f'wait {sec} seconds', end = '\r')
    zictos
    zictos
    OP
    Sep 24, 2020
    4
    import time
    for i in range(10,0,-1):
    print('还需等待%s 秒' % i, end = '\r')
    time.sleep(1)
    print('还需等待 0 秒',end = '\r')
    zictos
    zictos
    OP
    Sep 24, 2020
    5
    import time
    for i in range(10,0,-1):
    [缩进]print('还需等待%s 秒' % i, end = '\r')
    [缩进]time.sleep(1)
    print('还需等待 0 秒',end = '\r')
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   978 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 22:35 · PVG 06:35 · LAX 14:35 · JFK 17:35
    ♥ Do have faith in what you're doing.