How To Run A Python Program At A Specific Time

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone thank you for joining again my name is Taylor and today I'm going to teach you how to use time to call your programs at a specific time first what you want to do is open up your favorite text editor I'm going to be using Kate today I've already created a python file called _ program.i first let's import the time module so we'll say import time now hit enter twice and let's create our main function after we create our main function we are going to utilize the time module so first what we want to do is create a variable to store the local time let's give this variable a name called local time so we'll say local uncore time is equal to now we'll call our module time do local time open and close parentheses so what this is saying is from the time module get our current local time from our PC once we do that we're going to store it into the local time variable let's hit enter again now we need to format the local time so that way we can call our functions at a specific time we'll call this formator time is equal to time dot St trf time open and close parentheses in those parentheses open and close quotations in those quotations let's send in a percent H for the hour a percent M for the minute and a percent s so these are all capitalized now we need to go outside of the quotations at a comma and then we're going to send in local uncore time let's save this for now and then let's hit enter a few times now we can print this out to see what our formatted time looks like so we can say print open and close parentheses format time now let's go to our terminal and type python space and then the name of our file as you can see it's showing the current time in military time so now that we know that our current time is going to be in military time we need to create a variable that stores the time we want our program to run and it needs to be represented in military time let's create a variable now we'll go below format time and say stop uncore time is equal to open and close quotations in those quotations we'll specify a time we want it to stop so let's say we wanted to stop it at 1900 hours so we'll say 19 then separate it with a colon Z 0 separate with a colon again 0 0 so let's save this now we can add a conditional statement by saying if open and close parentheses format underscore time double equals stop uncore time add a colon at the end and let's say print open and close quotations the times matched and let's move this stop time to the top now let's create a while loop we'll say while one add a colon here and let's indent this and let's save this and if we were to run this program we can see the time changing so let's clear the screen rerun the program as you can see the time is changing constantly so let's contrl C this and then contrl L to clear the screen what if we wanted the time to be shown on a single line rather than multiple lines we can do that by going into this print statement of formator time at a comma say end is equal to open and close quotations in those quotations we'll say back SLR and let's save this and let's rerun the program as you can see every second this is changing and we we are not adding a new line now let's go ahead and add a break statement in the while loop we can do that by going inside of this if statement and say break and let's save this and now once the format time is equal to the stop time then the times will match and the while loop will break once the W Loop breaks let's add print the w F Loop broke let's save this another thing to note is in this if statement this is where you want to add your function to be called now let's run this program clear the screen hit up on our Arrow key and hit enter you can see the program stopped because the format time was equal to our stop time so we want to call a function when our format time is equal to our stop time so this is how you would use time to call a function at a certain time of day this is useful when you want a scheduled task thank you all for watching I invite you to subscribe and I hope to see you next time
Info
Channel: Taylor's Software
Views: 1,660
Rating: undefined out of 5
Keywords:
Id: PWCpRR-Xw7A
Channel Id: undefined
Length: 5min 48sec (348 seconds)
Published: Sun May 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.