Python - 时间处理与定时任务

1.计算明天和昨天的日期

# 获取今天、昨天和明天的日期
# 引入datetime模块
import datetime
#计算今天的时间
today = datetime.date.today()
#计算昨天的时间
yesterday = today - datetime.timedelta(days = 1)
#计算明天的时间
tomorrow = today + datetime.timedelta(days = 1)
#打印这三个时间
print(yesterday,today, tomorrow)

2.计算上一个的时间

方法一:计算上一个的时间

# 计算上一个的时间
# 引入datetime,calendar两个模块
import datetime, calendar last_friday = datetime.date.today()
oneday = datetime.timedelta(days=1) while last_friday.weekday() != calendar.FRIDAY:
last_friday -= oneday print(last_friday.strftime('%A, %d-%b-%Y'))

方法二:借助模运算寻找上一个星期五

# 借助模运算,可以一次算出需要减去的天数,计算上一个星期五
# 同样引入datetime,calendar两个模块
import datetime
import calendar today = datetime.date.today()
target_day = calendar.FRIDAY
this_day = today.weekday()
delta_to_target = (this_day - target_day) % 7
last_friday = today - datetime.timedelta(days=delta_to_target) print(last_friday.strftime("%d-%b-%Y"))

3.计算歌曲的总播放时间

# 获取一个列表中的所有歌曲的播放时间之和
import datetime def total_timer(times):
td = datetime.timedelta(0)
duration = sum([datetime.timedelta(minutes=m, seconds=s) for m, s in times], td)
return duration times1 = [(2, 36),
(3, 35),
(3, 45),
]
times2 = [(3, 0),
(5, 13),
(4, 12),
(1, 10),
] assert total_timer(times1) == datetime.timedelta(0, 596)
assert total_timer(times2) == datetime.timedelta(0, 815) print("Tests passed.\n"
"First test total: %s\n"
"Second test total: %s" % (total_timer(times1), total_timer(times2)))

4.反复执行某个命令

 # 以需要的时间间隔执行某个命令  

 import time, os  

 def re_exe(cmd, inc = 60):
while True:
os.system(cmd);
time.sleep(inc) re_exe("echo %time%", 5)

5.定时任务

 #这里需要引入三个模块
import time, os, sched # 第一个参数确定任务的时间,返回从某个特定的时间到现在经历的秒数
# 第二个参数以某种人为的方式衡量时间
schedule = sched.scheduler(time.time, time.sleep) def perform_command(cmd, inc):
os.system(cmd) def timming_exe(cmd, inc = 60):
# enter用来安排某事件的发生时间,从现在起第n秒开始启动
schedule.enter(inc, 0, perform_command, (cmd, inc))
# 持续运行,直到计划时间队列变成空为止
schedule.run() print("show time after 10 seconds:")
timming_exe("echo %time%", 10)

6.利用sched实现周期调用

 import time, os, sched  

 # 第一个参数确定任务的时间,返回从某个特定的时间到现在经历的秒数
# 第二个参数以某种人为的方式衡量时间
schedule = sched.scheduler(time.time, time.sleep) def perform_command(cmd, inc):
# 安排inc秒后再次运行自己,即周期运行
schedule.enter(inc, 0, perform_command, (cmd, inc))
os.system(cmd) def timming_exe(cmd, inc = 60):
# enter用来安排某事件的发生时间,从现在起第n秒开始启动
schedule.enter(inc, 0, perform_command, (cmd, inc))
# 持续运行,直到计划时间队列变成空为止
schedule.run() print("show time after 10 seconds:")
timming_exe("echo %time%", 10)

Python - 时间相关与计划任务的更多相关文章

  1. Python核心团队计划2020年停止支持Python2,NumPy宣布停止支持计划表

    Python核心团队计划在2020年停止支持Python 2.NumPy项目自2010年以来一直支持Python 2和Python 3,并且发现支持Python 2对我们有限的资源增加了负担:因此,我 ...

  2. python性能测试大致计划

      hi guy: 如果注意到创建时间,那就对了.这份文章,是我学习Python一个月以后动手写的.   写下这份计划以后,只完成了第一步,其中磕磕绊绊编写代码的过程,很大一部分时间是完全用txt写的 ...

  3. windows开机自启python服务(任务计划程序+bat脚本)

    需求:根据上海某银行 的需求,使用到获取数据服务的软件 只能在windows上运行,所以有 windows系统开机用户登录后自启动python flask服务 的需求: 相关工具:win10系统中,使 ...

  4. 【head first python】学习计划

    1 初识Python:人人都爱列表 2 共享你的代码:函数模块 3 文件与异常:处理错误 4 持久存储:数据保存到文件 5 推导数据:处理数据! 6 定制数据对象:打包代码与数据 7 Web开发:集成 ...

  5. Python百题计划

    一.基础篇 想要像类似执行shell脚本一样执行Python脚本,需要在py文件开头加上什么?KEY:#!/usr/bin/env python Python解释器在加载 .py 文件中的代码时,会对 ...

  6. Python的学习计划

    整体进度(6-7个月毕业)一.(2月左右)Python基础二.数据库(1-2周)---存储数据和信息(本质上和文件没有区别) 增删改查更方便了三.前端(2周左右)---html.css等等四.框架(2 ...

  7. python 百题计划

    一.基础篇 想要像类似执行shell脚本一样执行Python脚本,需要在py文件开头加上什么?KEY:#!/usr/bin/env python Python解释器在加载 .py 文件中的代码时,会对 ...

  8. 吴裕雄--天生自然python学习笔记:python通过“任务计划程序”实现定时自动下载或更新运行 PM2.5 数据抓取程序数据

    在 Windows 任务计划程序中,设置每隔 30 分钟自动抓取 PM2.5 数据,井保存 在 SQLite 数据库中 . import sqlite3,ast,requests,os from bs ...

  9. thinkphp5+python.apscheduler实现计划任务

    1.thinkphp5配置自定义命令行 /application/console/command namespace app\console\command; use think\console\Co ...

随机推荐

  1. 反射实体类拼接SQL语句

    实体类基类: using System; using System.Collections.Generic; using System.Linq; using System.Reflection; u ...

  2. Set a One-to-Many Relationship设置一对多关系 (XPO)

    In this lesson, you will learn how to set a one-to-many relationship between business objects. The C ...

  3. Android 安全攻防(三): SEAndroid Zygote

    转自:http://blog.csdn.net/yiyaaixuexi/article/details/8495695 在Android系统中,所有的应用程序进程,以及系统服务进程SystemServ ...

  4. Sublime Text 3 插件安装、搭建Python、Java开发环境

    一.下载并安装 Sublime Text 3 官网地址:http://www.sublimetext.com目前最新版本为:3.2.1 (BUILD 3207)安装时勾选“Add to explore ...

  5. mysql操作数据表

    目录 创建数据表 列约束 查看数据表结构 列类型(字段类型) 整型 浮点型 字符串 时间日期类型 Date Time Datetime Timestamp Year 枚举enum 修改表名 增加字段 ...

  6. windows linux 通过SSH X11Forwrding 使用图形化界面

    有时候,我们需要在命令行中使用远程的GUI程序,这样我们就需要x11转发的来进行访问: Linux平台下不需要特别的配置,假如我们要远程的机器是centos机器,只要做如下配置即可: #vi /etc ...

  7. 『008』Zabbix

    『006』索引-Monitoring Zabbix [001]- 点我快速打开文章[001-Zabbix 服务安装] [002]- 点我快速打开文章[002-Zabbix 前端配置] 更新中

  8. spark 基础

    scala版 ,基本名词概念及 rdd的基本创建及使用 var conf = new SparkConf() var sc: SparkContext = new SparkContext(conf) ...

  9. CSS百分比padding实现比例固定图片自适应布局

    一.CSS百分比padding都是相对宽度计算的 在默认的水平文档流方向下,CSS margin和padding属性的垂直方向的百分比值都是相对于宽度计算的,这个和top, bottom等属性的百分比 ...

  10. input 控件常用属性