1. Calculate the day of the week
When your girlfriend is going to have a birthday, you must make a reservation for a restaurant to celebrate, restaurants will be empty weekdays, weekend location is not good, if you can know her birthday is a few weeks, the following procedure can be fixed ~ ~
girl friend's birthday, for instance, is gf_birthday= ' 2017-3-3 '.
1. We first format the variable as a DateTime object
Birthday=datetime.datetime.strptime (Gf_birthday, '%y-%m-%d ')
2). Then use the function weekday in the DateTime to get a subscript
Birthday.weekday ()
3. Construct a list of weekdays from the list according to the subscript is the week
weekdays=[' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday ', ' Sunday '
Weekdays[birthday.weekday ()]
Of course, you have to calculate such as Valentine's Day, Christmas or anything can be used above the program, or the entire list of 10 years of the festival are listed in the calculation can be, is not very simple, the understanding of the date wood has deepened a bit under
2. timed Tasks
In Python, for example, you want to regularly climb a Web page, or do download students want to go to a regular day at 12 o ' clock a file, or regularly to scan some servers , even the boss's need to constantly change may be, every 5 minutes, Or 10 points per hour, every month there are scheduled tasks
How to use Python to break is very simple, the following program easy to handle
Let's start with the simplest example, assuming that we're in the 10th second of every minute, to perform a task to print the current directory
1. Window below is dir command, Linux is LS
We use the Platform module to judge the operating system.
Import Platform
Os_platfrom=platform.platform ()
If Os_platfrom.startswith (' Darwin '):
print ' This is Mac OS system '
Os.system (' ls ')
Elif os_platfrom.startswith (' Window '):
Print ' This is win system '
Os.system (' dir ')
2. How to perform regularly
A. We first get the current time
Now=datetime.datetime.now ()
Assuming the current time is 2017-02-09 20:19:47.555000
B. Then we enter a target time that you want to perform at timed intervals
Like if you were X minutes 10 seconds to execute Sched_timer=datetime.datetime (x,x,x,x,x,10)
The previous x is not important (as long as the last 10 seconds), we set the target time than the current a bit later can:
Sched_timer=datetime.datetime (2017,2,9,20,20)
C. Okay, when it's time to 20:20:10, run our program.
How is it timed to come, very simple to use
If Now==sched_timer:
' Run Task '
D. So how to allow time to continue in the next 一分钟10秒, but also very simple with timedelta ()
Datetime.timedelta (Minutes=1) increases target time by one minute
Sched_timer=sched_timer+datetime.timedelta (Minutes=1)
And then the outside with a while dead loop hold to live on it
The same code can also be extended, the Minutes=1 changed into Hours=1 to become an hourly task, changed to Days=1 to become a daily scheduled task author: Rookie learn Python link: http://www.jianshu.com/ P/AB8D9E576AC4 Source: Jane Book copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.
The following is the comments of this article, I think it is good, but also solve the problem of the above time mismatch problem, specially posted come to see
according to the method provided by the landlord to try, the landlord provides a kind of timing thinking is feasible, but in the implementation of the "if Now==sched_timer:" Here may be a bit of a problem.
in the while Loop, Datetime.datetime.now () Gets the time "2017-8-10 17:22:54.123456" format, while the landlord set the time format print out by default is "2017-8-10 17:22:54 ", may be the factor of the program running time, the actual running process to determine the two equal, and then execute the statement within the if the situation is rarely (I tried here half the afternoon, not once to determine the two equal).
The following are my own minor modifications to use:
Import time
Sched_time = datetime.datetime (2017, 8, 0)
Loopflag = 0< Br>while True:
now = Datetime.datetime.now ()
If sched_time<now< (Sched_time+datetime.timedelta (seconds= 1):
Loopflag = 1
time.sleep (1)
If Loopflag = 1:
func () #此处为你自己想定时执行的功能函数
Loopflag = 0