Python3中常用的處理時間和實現(xiàn)定時任務(wù)的方法的介紹
無論哪種編程語言,時間肯定都是非常重要的部分,今天來看一下python如何來處理時間和python定時任務(wù),注意咯:本篇所講是python3版本的實現(xiàn),在python2版本中的實現(xiàn)略有不同,有時間會再寫一篇以便大家區(qū)分。
1.計算明天和昨天的日期
#! /usr/bin/env python #coding=utf-8 # 獲取今天、昨天和明天的日期 # 引入datetime模塊 import datetime #計算今天的時間 today = datetime.date.today() #計算昨天的時間 yesterday = today - datetime.timedelta(days = 1) #計算明天的時間 tomorrow = today + datetime.timedelta(days = 1) #打印這三個時間 print(yesterday, today, tomorrow)
2.計算上一個的時間
方法一:
#! /usr/bin/env python
#coding=utf-8
# 計算上一個的時間
#引入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'))
方法二:借助模運算尋找上一個星期五
#! /usr/bin/env python
#coding=utf-8
# 借助模運算,可以一次算出需要減去的天數(shù),計算上一個星期五
#同樣引入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.計算歌曲的總播放時間
#! /usr/bin/env python
#coding=utf-8
# 獲取一個列表中的所有歌曲的播放時間之和
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.反復(fù)執(zhí)行某個命令
#! /usr/bin/env python
#coding=utf-8
# 以需要的時間間隔執(zhí)行某個命令
import time, os
def re_exe(cmd, inc = 60):
while True:
os.system(cmd);
time.sleep(inc)
re_exe("echo %time%", 5)
5.定時任務(wù)
#! /usr/bin/env python
#coding=utf-8
#這里需要引入三個模塊
import time, os, sched
# 第一個參數(shù)確定任務(wù)的時間,返回從某個特定的時間到現(xiàn)在經(jīng)歷的秒數(shù)
# 第二個參數(shù)以某種人為的方式衡量時間
schedule = sched.scheduler(time.time, time.sleep)
def perform_command(cmd, inc):
os.system(cmd)
def timming_exe(cmd, inc = 60):
# enter用來安排某事件的發(fā)生時間,從現(xiàn)在起第n秒開始啟動
schedule.enter(inc, 0, perform_command, (cmd, inc))
# 持續(xù)運行,直到計劃時間隊列變成空為止
schedule.run()
print("show time after 10 seconds:")
timming_exe("echo %time%", 10)
6.利用sched實現(xiàn)周期調(diào)用
#! /usr/bin/env python
#coding=utf-8
import time, os, sched
# 第一個參數(shù)確定任務(wù)的時間,返回從某個特定的時間到現(xiàn)在經(jīng)歷的秒數(shù)
# 第二個參數(shù)以某種人為的方式衡量時間
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用來安排某事件的發(fā)生時間,從現(xiàn)在起第n秒開始啟動
schedule.enter(inc, 0, perform_command, (cmd, inc))
# 持續(xù)運行,直到計劃時間隊列變成空為止
schedule.run()
print("show time after 10 seconds:")
timming_exe("echo %time%", 10)
- Python實現(xiàn)定時任務(wù)
- Linux下Python腳本自啟動與定時任務(wù)詳解
- 詳解使用python crontab設(shè)置linux定時任務(wù)
- Python中定時任務(wù)框架APScheduler的快速入門指南
- python Celery定時任務(wù)的示例
- Python定時任務(wù)sched模塊用法示例
- Python3實現(xiàn)定時任務(wù)的四種方式
- 解決Python中定時任務(wù)線程無法自動退出的問題
- Linux部署python爬蟲腳本,并設(shè)置定時任務(wù)的方法
- python BlockingScheduler定時任務(wù)及其他方式的實現(xiàn)
相關(guān)文章
Python?Pandas實現(xiàn)將字符串格式轉(zhuǎn)為日期時間格式
日期和時間數(shù)據(jù)在數(shù)據(jù)分析和處理中起著關(guān)鍵作用,本文將詳細介紹如何使用Pandas將字符串格式的日期時間數(shù)據(jù)轉(zhuǎn)換為日期時間格式,需要的可以參考下2024-01-01
使用Python實現(xiàn)簡單的數(shù)據(jù)備份
數(shù)據(jù)備份,即數(shù)據(jù)的復(fù)制和存儲,是指將數(shù)據(jù)從一個位置復(fù)制到另一個位置,以防止原始數(shù)據(jù)丟失或損壞,下面我們就來了解一下用Python如何實現(xiàn)這一功能吧2025-03-03
Python?中?Selenium?的?send_keys()?函數(shù)用法小結(jié)
send_keys() 是將數(shù)字、文本和符號等鍵盤輸入發(fā)送到應(yīng)用程序的文本框的過程, send_keys() 是 WebDriver 的一部分,每個鍵盤輸入都會發(fā)送到此元素,這篇文章主要介紹了Python?中?Selenium?的?send_keys()?函數(shù),需要的朋友可以參考下2023-11-11
Python-re中search()函數(shù)的用法詳解(查找ip)
這篇文章主要介紹了Python-re中search()函數(shù)的用法-----查找ip,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
關(guān)于Django Models CharField 參數(shù)說明
這篇文章主要介紹了關(guān)于Django Models CharField 參數(shù)說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python實現(xiàn)將xml導(dǎo)入至excel
本文給大家講解的是使用Python的Testlink實現(xiàn)將實現(xiàn)將xml導(dǎo)入至excel表格中,方法非常的簡單,另外附上其他小伙伴的方法,有需要的童鞋們可以參考下。2015-11-11
Python報錯ImportError: No module named ‘mi
在 Python 開發(fā)過程中,報錯是常有的事,而當遇到“ImportError: No module named ‘missing_module’”這樣的報錯時,可能會讓開發(fā)者感到困惑和苦惱,本文將深入探討這個報錯的原因和解決方法,幫助開發(fā)者快速解決這個問題,需要的朋友可以參考下2024-10-10
Python 2.6.6升級到Python2.7.15的詳細步驟
這篇文章主要介紹了Python 2.6.6升級到Python2.7.15的詳細步驟,本文分步驟給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12

