Python控制台輸出時刷新當前行內容而不是輸出新行


需求目標

執行Python程序的時候在控制台輸出內容的時候固定一刷新內容,如下:

Downloading File FooFile.txt [47%]

而不是

Downloading File FooFile.txt [47%]
Downloading File FooFile.txt [48%]
Downloading File FooFile.txt [49%]

 

實現上很簡單

只要增加一個 end='\r' 的print參數

import time
for i in range(100):
    time.sleep(0.1)
    print("\r Downloading File FooFile.txt [{}%] ".format(100-i),end='\r')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM