发布时间:2019-09-12 07:59:42编辑:auto阅读(1875)
昨天写了个计算距离今年春节还有几天的脚本,今天在群里看到可以实时刷新的一个脚本,利用flush()函数清空输出的功能,我把昨天的脚本修改了以下,下面直接上代码:
[root@localhost progressbar]# cat spring.py
#coding=utf-8
#!/usr/bin/env python
import datetime
import sys
import time
spring=datetime.datetime(2014,1,31,0,0,0)    #春节日期
while  True:
        pass
        today=datetime.datetime.now()        #今天是几月几号
        day=(spring-today).days              #得到还有几天
        second=(spring-today).seconds     #得到还有几秒
        sec=second%60                   #根据秒数得到还有几秒
        minute=second/60%60             #根据秒得到分钟数
        hour=second/60/60        #根据秒数得到小时
        if hour>24:
                hour=hour-24    #如果超过24小时,就要算超过1天,所以要减去24
        sys.stdout.write( "离今年春节还有"+str(day)+"天"+str(hour)+"小时"+str(mi
nute)+"分钟"+str(sec)+"秒"+'\r')
#       sys.stdout.write( "离今年春节还有 %d 天 %d 小时 %d 分钟 %d 秒 \r "  %(da
y,hour,minute,sec) )
        sys.stdout.flush()
        time.sleep(1)
[root@localhost progressbar]#下面就看看效果吧:
效果不错,可以实时显示,感兴趣的朋友可以拷贝下脚本试试
上一篇: 利用Python生成随机4位验证码
下一篇: CentOS7修复python拯救yum
 51191
 50601
 41228
 38046
 32510
 29416
 28277
 23129
 23091
 21424
 1476°
 2194°
 1818°
 1748°
 2059°
 1810°
 2493°
 4183°
 4046°
 2888°