推荐学习书目
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 · 2473 views
    This topic created in 2061 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
        1
    xe2vherd  
       Sep 24, 2020 via iPhone
    退格符了解下
    zackwu
        2
    zackwu  
       Sep 24, 2020
    \r
    jackmod
        3
    jackmod  
       Sep 24, 2020
    print(f'wait {sec} seconds', end = '\r')
    zictos
        4
    zictos  
    OP
       Sep 24, 2020
    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
       Sep 24, 2020
    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   ·   Solana   ·   3088 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 12:43 · PVG 20:43 · LAX 05:43 · JFK 08:43
    ♥ Do have faith in what you're doing.