Get today 、 The dates of yesterday and tomorrow
# introduce datetime modular
import datetime
# Calculate today's time
today = datetime.date.today()
# Calculate yesterday's time
yesterday = today - datetime.timedelta(days = 1)
# Calculate tomorrow's time
tomorrow = today + datetime.timedelta(days = 1)
# Print these three times
print(yesterday, today, tomorrow)
Method 1 :
Calculate the last time
introduce datetime,calendar Two modules
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'))
Method 2 : Find the last Friday with the help of modular operation
With the help of modular operation , You can calculate the number of days to subtract at a time , Calculate the last Friday
Also introduce datetime,calendar Two modules
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"))
Get the sum of playing time of all songs in a list
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)))
Execute a command at the required interval
import time, os
def re_exe(cmd, inc = 60):
while True:
os.system(cmd);
time.sleep(inc)
re_exe("echo %time%", 5)
Three modules need to be introduced here
''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :711312441 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
import time, os, sched
# The first parameter determines the time of the task , Returns the number of seconds elapsed from a specific time to the present
# The second parameter measures time in some artificial way
schedule = sched.scheduler(time.time, time.sleep)
def perform_command(cmd, inc):
os.system(cmd)
def timming_exe(cmd, inc = 60):
# enter Used to schedule an event , From now on n Start in seconds
schedule.enter(inc, 0, perform_command, (cmd, inc))
# Keep running , Until the scheduled time queue becomes empty
schedule.run()
print("show time after 10 seconds:")
timming_exe("echo %time%", 10)
import time, os, sched
# The first parameter determines the time of the task , Returns the number of seconds elapsed from a specific time to the present
# The second parameter measures time in some artificial way
schedule = sched.scheduler(time.time, time.sleep)
def perform_command(cmd, inc):
# arrange inc Seconds later, run yourself again , I.e. periodic operation
schedule.enter(inc, 0, perform_command, (cmd, inc))
os.system(cmd)
def timming_exe(cmd, inc = 60):
# enter Used to schedule an event , From now on n Start in seconds
schedule.enter(inc, 0, perform_command, (cmd, inc))
# Keep running , Until the scheduled time queue becomes empty
schedule.run()
print("show time after 10 seconds:")
timming_exe("echo %time%", 10)
Catalog One 、 Two 、python Inp
5、 ... and 、 Upload files (For