简体   繁体   中英

How to schedule a Python script to run at a certain time?

Hello I am very new to programming and i'm trying to make an auto-posting bot for my subreddit.I'm using praw and I need to run this script at certain times and have it input and work

import praw

r = praw.Reddit(user_agent="UA")
r.login("username", "password")
sub = r.get_subreddit("Sub")
sub.submit("Title", text="Post text")

I'm running windows and someone said to use the task scheduler but I haven't been able to figure it out. Any help would be great. Thank You.

I would suggest to look into sched, a general purpose event scheduler. It is described, with appropriate examples to start you, in the Python's documentation .

Sample:

import time
import sched

scheduler = sched.scheduler(time.time, time.sleep)

def reddit():
  <your code>

def scheduler_reddit():

  scheduler.enter(0, 1, reddit, ())
  scheduler.run()
  time.sleep(3600)

for i in range(100):
  scheduler_reddit()

Change 3600 with the desired time in seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM