Output to the same line overwriting previous

前端 未结 4 679
醉话见心
醉话见心 2020-11-27 06:02

how to get output to the same line by overwriting previous received Timing(countdown) by NTP server. As shown below after each second timing is receiving in next row.

<
4条回答
  •  伪装坚强ぢ
    2020-11-27 06:55

    I am using python 2.7

    python --version
    Python 2.7.12 :: Anaconda 4.1.1 (64-bit)
    

    and I am using this the following function as a hook to display the download progress, by using urllib.urlretrieve

    def hook(bcount , bsize, tsize ):
        str = "[ %3d%% of %10d] \r" % (bcount * bsize * 100/tsize , tsize)    
        print str,
    
    urllib.urlretrieve (url, file_name, hook)
    

    Explanation: the \r put the cursor at start of the line , and the comma avoid to print in a new line , if you have the same number of character for each print , this will do the trick

    if you are curious about urllib and the hook I am using you'll find the doc https://docs.python.org/2/library/urllib.html

提交回复
热议问题