0

I want that a specific time of the day (for example 10:00:00), one of my if condition activates.

For example:

if time is 10:00:00: print("Hello world")

Imortant: I already read this: Python script to do something at the same time every day

But I don't want to use a function!

Trees Jin
  • 13
  • 3
  • why don't you want to use a function? And you can just have a while True loop, check if it is your desired time, if not, sleep – Mahrkeenerh Nov 04 '21 at 12:02
  • 1
    A solution can be to have your script ran by crontab. – MSH Nov 04 '21 at 12:03
  • @Mahrkeenerh because I have multiple conditions: if x = 3 and time = 10:10:10: do this specified job, how can i do this with a function – Trees Jin Nov 04 '21 at 12:11
  • You can do something like this `if datetime.now().strftime("%H:%M:%S") == '10:00:00':` https://www.programiz.com/python-programming/datetime/current-time – Sefan Nov 04 '21 at 12:15
  • you can do the job if time is right, and inside the job check if x is 3 – Mahrkeenerh Nov 04 '21 at 12:18
  • @Mahrkeenerh you mean this way: import schedule import time def job(t): if x = 3: print "I'm working...", t return schedule.every().day.at("01:00").do(job,'It is 01:00') while True: schedule.run_pending() time.sleep(60) # wait one minute – Trees Jin Nov 04 '21 at 12:25
  • probably, but the formatting is terrible in a comment, so can't say for sure – Mahrkeenerh Nov 04 '21 at 12:26

2 Answers2

1

If you do not one to use a function but need to run a simple script at certain times, you may use crons/job schedulers for this.

Windows and Linux both supports cron operations.

If you want to do this programmatically instead of relying on operating system tools you need to write a service or a long running process for it.

1

You could easy use datetime to help you with that.

import datetime
from time import sleep

timing = [10, 0, 0] # Hour, minute, second, in 24 hour time

while True: # Repeat forever
    now = datetime.datetime.now()
    data = [now.hour, now.minute, now.second]
    if data == timing:
        # Code to be executed
        print("Hello World")
        #######
        sleep(1) # To ensure the command is not repeated again
        # break # Uncomment this if you want to execute the command only once

Make sure that I indented it properly, because one space can tick python off :).

The way that it works: import datetime and from time import sleep import the necessary modules and functions that you will need.

Modules needed: datetime time.sleep

Now we're set. timing = [10,0,0] sets the time that you want to use (you'll see why later)

while True repeats the loop... on and on and on.

now = datetime.datetime.now() creates a shortcut for such a long piece of text.

data == timing makes sure the time matches the timing you asked.

Note that the timing is in UTC Go to Getting the correct timezone offset in Python using local timezone to know how to find your offset.

An offset of UTC-0200 (Or -7200 seconds) means that you need to ADD 2 hours to your time to get UTC. Or, if your time zone is UTC+0200, SUBSTRACT 2 hours from your time.

Lucas Urban
  • 627
  • 4
  • 15