0
import time
import webbrowser

print(time.ctime())

targetTime = time.ctime()


if(targetTime == "Tue May 01 11:05:17 2018"):    
    webbrowser.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ")

This is what I tried already and it doesn't open the link when the time comes. I read through the time library but I couldn't find anything to help me. My target is for the program to open a link at a time that I want. Any help appreciated.

Bogdan
  • 27
  • 1
  • 1
  • 2
  • In general, programming is not magic. Unless you keep updating the time (and comparing it to a time instead of a string), this won't work. – Mad Physicist May 01 '18 at 18:11
  • I did just started a basic Python tutorial where he uses time.sleep in order to achieve the same thing but my problem is that I might not start the program at the same time as yesterday so that will mess me up. That is why i asked here, because I want to run the program and it will do my function at a time I want. – Bogdan May 01 '18 at 18:20

2 Answers2

9

Python comes built-in with a simple scheduling library called sched that supports this.

import sched, time

def action():
    webbrowser.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ")

# Set up scheduler
s = sched.scheduler(time.localtime, time.sleep)
# Schedule when you want the action to occur
s.enterabs(time.strptime('Tue May 01 11:05:17 2018'), 0, action)
# Block until the action has been run
s.run()
scnerd
  • 5,433
  • 2
  • 21
  • 34
4

If you don't mind using third party modules, there's Python pause:

import pause
from datetime import datetime

pause.until(datetime(2018, 5, 1, 11, 5, 17))
webbrowser.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
fferri
  • 16,928
  • 4
  • 40
  • 82